Add SMaterial::IUserData to make it easier passing additional values to shaders

Irrlicht generally avoided user pointers in the past, but after trying all kind of ugly workarounds - this is just easier and
not that much downsides really. Tiny speed costs due to additional SMaterial memory size and new comparison tests. 
But allows to keep SMaterial alive and useful for a while longer without needing a complete rewrite and it can now be used for stuff like writing PBR shaders.
Using a new interface io::IUserData for this which also allows serialization the user data (that part is untested so far, sorry)


git-svn-id: svn://svn.code.sf.net/p/irrlicht/code/trunk@6567 dfc29bdd-3216-0410-991c-e03cc46cb475
This commit is contained in:
cutealien 2023-11-07 15:43:49 +00:00
parent d0605cf397
commit 3c9a856e6d
20 changed files with 155 additions and 6 deletions

@ -1,6 +1,7 @@
--------------------------
Changes in 1.9 (not yet released)
- Add io::IUserData which can be set in SMaterial to make it easer passing additional material values to shaders
- Add lens shift support for the camera and the perspective projection functions
- TGA loader no longer reduces 24&32 bit TGA's with palettes to 16 bit. Thanks @erlehmann for report: https://irrlicht.sourceforge.io/forum/viewtopic.php?t=52925
- Fix compile error with OS X 10.10 SDK, bug #463. Thanks @Ryan Schmidt for report and patch.

53
include/IUserData.h Normal file

@ -0,0 +1,53 @@
// This file is part of the "Irrlicht Engine".
// For conditions of distribution and use, see copyright notice in irrlicht.h
#ifndef IRR_I_USER_DATA_H_INCLUDED
#define IRR_I_USER_DATA_H_INCLUDED
#include "irrTypes.h"
namespace irr
{
namespace io
{
class IAttributes;
struct SAttributeReadWriteOptions;
//! Irrlicht may allow users to set their own data via those pointers
//! Irrlicht has no memory control over IUserData, the user is completely responsible for that
class IUserData
{
public:
//! To identify the class type.
//! You can for example use MAKE_IRR_ID to create four CC codes
virtual irr::u32 getType() const { return 0; }
//! To be overloaded if comparisons matter
//! You can then cast other to your derived class
virtual bool compare(const IUserData& other) const
{
return getType() == other.getType();
}
//! Writes data attributes
virtual void serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options=0) const {}
//! Reads data attributes
virtual void deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options=0) {}
//! Used internally by Irrlicht to check if data has changed
bool operator!=(const IUserData& other) const
{
return !compare(other);
}
protected:
// Irrlicht is never allowed to delete this
// If users want to delete such objects they should go over derived classes
~IUserData() {}
};
} // end namespace io
} // end namespace irr
#endif // IRR_I_USER_DATA_H_INCLUDED

@ -11,6 +11,7 @@
#include "EMaterialTypes.h"
#include "EMaterialFlags.h"
#include "SMaterialLayer.h"
#include "IUserData.h"
namespace irr
{
@ -313,7 +314,8 @@ namespace video
PolygonOffsetFactor(0), PolygonOffsetDirection(EPO_FRONT),
Wireframe(false), PointCloud(false), GouraudShading(true),
Lighting(true), ZWriteEnable(EZW_AUTO), BackfaceCulling(true), FrontfaceCulling(false),
FogEnable(false), NormalizeNormals(false), UseMipMaps(true)
FogEnable(false), NormalizeNormals(false), UseMipMaps(true),
UserData(0)
{ }
//! Texture layer array.
@ -493,6 +495,11 @@ namespace video
/** Sometimes, disabling mipmap usage can be useful. Default: true */
bool UseMipMaps:1;
//! Allow users to add their own material data
//! User is resonsible for the memory of this pointer
//! You should override IUserData::compare for user custom data, so SMaterial knows when it changes
io::IUserData* UserData;
//! Gets the texture transformation matrix for level i
/** \param i The desired level. Must not be larger than MATERIAL_MAX_TEXTURES
\return Texture matrix for texture level i. */
@ -713,7 +720,8 @@ namespace video
PolygonOffsetSlopeScale != b.PolygonOffsetSlopeScale ||
PolygonOffsetFactor != b.PolygonOffsetFactor ||
PolygonOffsetDirection != b.PolygonOffsetDirection ||
UseMipMaps != b.UseMipMaps
UseMipMaps != b.UseMipMaps ||
UserData != b.UserData || (UserData && b.UserData && *UserData != *b.UserData)
;
for (u32 i=0; (i<MATERIAL_MAX_TEXTURES_USED) && !different; ++i)
{

@ -154,6 +154,7 @@
#include "ITexture.h"
#include "ITimer.h"
#include "ITriangleSelector.h"
#include "IUserData.h"
#include "IVertexBuffer.h"
#include "IVideoDriver.h"
#include "IVideoModeList.h"

@ -2164,6 +2164,9 @@ io::IAttributes* CNullDriver::createAttributesFromMaterial(const video::SMateria
attr->addFloat("PolygonOffsetDepthBias", material.PolygonOffsetDepthBias);
attr->addFloat("PolygonOffsetSlopeScale", material.PolygonOffsetSlopeScale);
if ( material.UserData )
material.UserData->serializeAttributes(attr, options);
// TODO: Would be nice to have a flag that only serializes rest of texture data when a texture pointer exists.
prefix = "BilinearFilter";
for (i=0; i<MATERIAL_MAX_TEXTURES; ++i)
@ -2249,6 +2252,9 @@ void CNullDriver::fillMaterialStructureFromAttributes(video::SMaterial& outMater
outMaterial.PolygonOffsetDepthBias = attr->getAttributeAsFloat("PolygonOffsetDepthBias", outMaterial.PolygonOffsetDepthBias);
outMaterial.PolygonOffsetSlopeScale = attr->getAttributeAsFloat("PolygonOffsetSlopeScale", outMaterial.PolygonOffsetSlopeScale);
if ( outMaterial.UserData )
outMaterial.UserData->deserializeAttributes(attr);
prefix = "BilinearFilter";
if (attr->existsAttribute(prefix.c_str())) // legacy
outMaterial.setFlag(EMF_BILINEAR_FILTER, attr->getAttributeAsBool(prefix.c_str()));

@ -421,6 +421,7 @@
<Unit filename="../../include/EReadFileType.h" />
<Unit filename="../../include/ESceneNodeAnimatorTypes.h" />
<Unit filename="../../include/ESceneNodeTypes.h" />
<Unit filename="../../include/ESceneNodeUpdateAbs.h" />
<Unit filename="../../include/EShaderTypes.h" />
<Unit filename="../../include/ETerrainElements.h" />
<Unit filename="../../include/IAnimatedMesh.h" />
@ -1014,6 +1015,7 @@
<Unit filename="IDepthBuffer.h" />
<Unit filename="IImagePresenter.h" />
<Unit filename="ITriangleRenderer.h" />
<Unit filename="IUserData.h" />
<Unit filename="IZBuffer.h" />
<Unit filename="Irrlicht.cpp" />
<Unit filename="Octree.h" />

@ -871,6 +871,7 @@
<ClInclude Include="..\..\include\IRenderTarget.h" />
<ClInclude Include="..\..\include\IrrCompileConfig.h" />
<ClInclude Include="..\..\include\irrlicht.h" />
<ClInclude Include="..\..\include\IUserData.h" />
<ClInclude Include="..\..\include\leakHunter.h" />
<ClInclude Include="..\..\include\IrrlichtDevice.h" />
<ClInclude Include="..\..\include\irrTypes.h" />

@ -1446,6 +1446,9 @@
<ClInclude Include="CBufferRenderNode.h">
<Filter>Irrlicht\scene\sceneNodes</Filter>
</ClInclude>
<ClInclude Include="..\..\include\IUserData.h">
<Filter>include\io</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="..\..\changes.txt">

@ -868,6 +868,7 @@
<ClInclude Include="..\..\include\IRenderTarget.h" />
<ClInclude Include="..\..\include\IrrCompileConfig.h" />
<ClInclude Include="..\..\include\irrlicht.h" />
<ClInclude Include="..\..\include\IUserData.h" />
<ClInclude Include="..\..\include\leakHunter.h" />
<ClInclude Include="..\..\include\IrrlichtDevice.h" />
<ClInclude Include="..\..\include\irrTypes.h" />

@ -1437,7 +1437,19 @@
</ClInclude>
<ClInclude Include="..\..\include\EDeviceTypes.h">
<Filter>include</Filter>
</ClInclude>
</ClInclude>
<ClInclude Include="..\..\include\EMeshBufferTypes.h">
<Filter>include\scene</Filter>
</ClInclude>
<ClInclude Include="..\..\include\ESceneNodeUpdateAbs.h">
<Filter>include\scene</Filter>
</ClInclude>
<ClInclude Include="CBufferRenderNode.h">
<Filter>Irrlicht\scene\sceneNodes</Filter>
</ClInclude>
<ClInclude Include="..\..\include\IUserData.h">
<Filter>include\io</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="..\..\changes.txt">

@ -868,6 +868,7 @@
<ClInclude Include="..\..\include\IRenderTarget.h" />
<ClInclude Include="..\..\include\IrrCompileConfig.h" />
<ClInclude Include="..\..\include\irrlicht.h" />
<ClInclude Include="..\..\include\IUserData.h" />
<ClInclude Include="..\..\include\leakHunter.h" />
<ClInclude Include="..\..\include\IrrlichtDevice.h" />
<ClInclude Include="..\..\include\irrTypes.h" />

@ -1437,7 +1437,19 @@
</ClInclude>
<ClInclude Include="..\..\include\EDeviceTypes.h">
<Filter>include</Filter>
</ClInclude>
</ClInclude>
<ClInclude Include="..\..\include\EMeshBufferTypes.h">
<Filter>include\scene</Filter>
</ClInclude>
<ClInclude Include="..\..\include\ESceneNodeUpdateAbs.h">
<Filter>include\scene</Filter>
</ClInclude>
<ClInclude Include="CBufferRenderNode.h">
<Filter>Irrlicht\scene\sceneNodes</Filter>
</ClInclude>
<ClInclude Include="..\..\include\IUserData.h">
<Filter>include\io</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="..\..\changes.txt">

@ -1438,6 +1438,18 @@
<ClInclude Include="..\..\include\EDeviceTypes.h">
<Filter>include</Filter>
</ClInclude>
<ClInclude Include="..\..\include\EMeshBufferTypes.h">
<Filter>include\scene</Filter>
</ClInclude>
<ClInclude Include="..\..\include\ESceneNodeUpdateAbs.h">
<Filter>include\scene</Filter>
</ClInclude>
<ClInclude Include="CBufferRenderNode.h">
<Filter>Irrlicht\scene\sceneNodes</Filter>
</ClInclude>
<ClInclude Include="..\..\include\IUserData.h">
<Filter>include\io</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="..\..\changes.txt">

@ -879,6 +879,7 @@
<ClInclude Include="..\..\include\IRenderTarget.h" />
<ClInclude Include="..\..\include\IrrCompileConfig.h" />
<ClInclude Include="..\..\include\irrlicht.h" />
<ClInclude Include="..\..\include\IUserData.h" />
<ClInclude Include="..\..\include\leakHunter.h" />
<ClInclude Include="..\..\include\IrrlichtDevice.h" />
<ClInclude Include="..\..\include\irrTypes.h" />

@ -1437,7 +1437,19 @@
</ClInclude>
<ClInclude Include="..\..\include\EDeviceTypes.h">
<Filter>include</Filter>
</ClInclude>
</ClInclude>
<ClInclude Include="..\..\include\EMeshBufferTypes.h">
<Filter>include\scene</Filter>
</ClInclude>
<ClInclude Include="..\..\include\ESceneNodeUpdateAbs.h">
<Filter>include\scene</Filter>
</ClInclude>
<ClInclude Include="CBufferRenderNode.h">
<Filter>Irrlicht\scene\sceneNodes</Filter>
</ClInclude>
<ClInclude Include="..\..\include\IUserData.h">
<Filter>include\io</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="..\..\changes.txt">

@ -606,6 +606,7 @@
<ClInclude Include="..\..\include\IRenderTarget.h" />
<ClInclude Include="..\..\include\IrrCompileConfig.h" />
<ClInclude Include="..\..\include\irrlicht.h" />
<ClInclude Include="..\..\include\IUserData.h" />
<ClInclude Include="..\..\include\leakHunter.h" />
<ClInclude Include="..\..\include\IrrlichtDevice.h" />
<ClInclude Include="..\..\include\irrTypes.h" />

@ -1438,6 +1438,18 @@
<ClInclude Include="..\..\include\EDeviceTypes.h">
<Filter>include</Filter>
</ClInclude>
<ClInclude Include="..\..\include\EMeshBufferTypes.h">
<Filter>include\scene</Filter>
</ClInclude>
<ClInclude Include="..\..\include\ESceneNodeUpdateAbs.h">
<Filter>include\scene</Filter>
</ClInclude>
<ClInclude Include="CBufferRenderNode.h">
<Filter>Irrlicht\scene\sceneNodes</Filter>
</ClInclude>
<ClInclude Include="..\..\include\IUserData.h">
<Filter>include\io</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="..\..\changes.txt">

@ -614,6 +614,7 @@
<ClInclude Include="..\..\include\IRenderTarget.h" />
<ClInclude Include="..\..\include\IrrCompileConfig.h" />
<ClInclude Include="..\..\include\irrlicht.h" />
<ClInclude Include="..\..\include\IUserData.h" />
<ClInclude Include="..\..\include\leakHunter.h" />
<ClInclude Include="..\..\include\IrrlichtDevice.h" />
<ClInclude Include="..\..\include\irrTypes.h" />

@ -1438,9 +1438,18 @@
<ClInclude Include="..\..\include\EDeviceTypes.h">
<Filter>include</Filter>
</ClInclude>
<ClInclude Include="..\..\include\EMeshBufferTypes.h">
<Filter>include\scene</Filter>
</ClInclude>
<ClInclude Include="..\..\include\ESceneNodeUpdateAbs.h">
<Filter>include\scene</Filter>
</ClInclude>
<ClInclude Include="CBufferRenderNode.h">
<Filter>Irrlicht\scene\sceneNodes</Filter>
</ClInclude>
<ClInclude Include="..\..\include\IUserData.h">
<Filter>include\io</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="..\..\changes.txt">

@ -1,4 +1,4 @@
Tests finished. 72 tests of 72 passed.
Compiled as DEBUG
Test suite pass at GMT Tue Oct 17 17:50:36 2023
Test suite pass at GMT Tue Nov 07 15:09:58 2023