Commit df0c9106 authored by D-AIRY's avatar D-AIRY

IXResourceTexture implementation

parent 507b9798
......@@ -54,6 +54,7 @@
<ClCompile Include="..\..\..\source\core\ResourceModel.cpp" />
<ClCompile Include="..\..\..\source\core\ResourceModelAnimated.cpp" />
<ClCompile Include="..\..\..\source\core\ResourceModelStatic.cpp" />
<ClCompile Include="..\..\..\source\core\ResourceTexture.cpp" />
<ClCompile Include="..\..\..\source\core\sxcore.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">false</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">false</ExcludedFromBuild>
......@@ -97,6 +98,7 @@
<ClInclude Include="..\..\..\source\core\ResourceModel.h" />
<ClInclude Include="..\..\..\source\core\ResourceModelAnimated.h" />
<ClInclude Include="..\..\..\source\core\ResourceModelStatic.h" />
<ClInclude Include="..\..\..\source\core\ResourceTexture.h" />
<ClInclude Include="..\..\..\source\core\sxcore.h" />
<ClInclude Include="..\..\..\source\core\Task.h" />
<ClInclude Include="..\..\..\source\core\TaskManager.h" />
......@@ -115,6 +117,7 @@
<ClInclude Include="..\..\..\source\xcommon\resource\IXModelProvider.h" />
<ClInclude Include="..\..\..\source\xcommon\resource\IXResourceManager.h" />
<ClInclude Include="..\..\..\source\xcommon\resource\IXResourceModel.h" />
<ClInclude Include="..\..\..\source\xcommon\resource\IXResourceTexture.h" />
</ItemGroup>
<PropertyGroup Label="Globals">
<ProjectGuid>{C1C1F046-C839-4602-AF70-923CDD237C1B}</ProjectGuid>
......
......@@ -91,6 +91,9 @@
<ClCompile Include="..\..\..\source\core\FolderPathsIterator.cpp">
<Filter>Source\FileSystem</Filter>
</ClCompile>
<ClCompile Include="..\..\..\source\core\ResourceTexture.cpp">
<Filter>Source\resource</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<Filter Include="Source">
......@@ -257,5 +260,11 @@
<ClInclude Include="..\..\..\source\core\FolderPathsIterator.h">
<Filter>Header\FileSystem</Filter>
</ClInclude>
<ClInclude Include="..\..\..\source\xcommon\resource\IXResourceTexture.h">
<Filter>Header\xcommon</Filter>
</ClInclude>
<ClInclude Include="..\..\..\source\core\ResourceTexture.h">
<Filter>Header\resource</Filter>
</ClInclude>
</ItemGroup>
</Project>
\ No newline at end of file
#include "ResourceTexture.h"
CResourceTexture2D::CResourceTexture2D():
BaseClass(GXTEXTURE_TYPE_2D)
{
}
CResourceTexture2D::~CResourceTexture2D()
{
mem_delete_a(pDataBlob);
}
UINT XMETHODCALLTYPE CResourceTexture2D::getWidth() const
{
return(m_uWidth);
}
UINT XMETHODCALLTYPE CResourceTexture2D::getHeight() const
{
return(m_uHeight);
}
void XMETHODCALLTYPE CResourceTexture2D::init(UINT uWidth, UINT uHeight, GXFORMAT format, UINT uMipmapCount, UINT uFrameCount)
{
assert(uWidth);
assert(uHeight);
assert(uMipmapCount > 0);
assert(uFrameCount > 0);
m_format = format;
m_uFrameCount = uFrameCount;
m_uMipmapCount = uMipmapCount;
m_uWidth = uWidth;
m_uHeight = uHeight;
mem_delete_a(pDataBlob);
if(uMipmapCount == IXRESOURCE_TEXTURE_AUTO_MIPS)
{
UINT uSize = min(uWidth, uHeight);
uMipmapCount = 1;
while((uSize >>= 1) >= 4)
{
++uMipmapCount;
}
}
size_t sizeData = uFrameCount * (sizeof(void*) + uMipmapCount * sizeof(XImageMip));
size_t sizeFrame = getTextureBytes(format, uWidth, uHeight);
size_t sizeTmp = sizeFrame;
for(UINT i = 0; i < uMipmapCount; ++i)
{
sizeData += sizeTmp * uFrameCount;
sizeTmp >>= 2;
}
pDataBlob = new byte[sizeData];
pppData = (XImageMip**)pDataBlob;
byte *pTmp = pDataBlob + sizeof(void*) * uFrameCount;
for(UINT i = 0; i < uFrameCount; ++i)
{
pppData[i] = (XImageMip*)pTmp;
pTmp += sizeof(void*) * uMipmapCount;
}
for(UINT i = 0; i < uFrameCount; ++i)
{
sizeTmp = sizeFrame;
for(UINT j = 0; i < uMipmapCount; ++i)
{
pppData[i][j].pData = pTmp;
pppData[i][j].sizeData = sizeTmp;
pppData[i][j].isWritten = false;
pTmp += sizeTmp;
sizeTmp >>= 2;
}
}
}
const IXResourceTexture2D* XMETHODCALLTYPE CResourceTexture2D::as2D() const
{
return(this);
}
IXResourceTexture2D* XMETHODCALLTYPE CResourceTexture2D::as2D()
{
return(this);
}
//##########################################################################
CResourceTextureCube::CResourceTextureCube():
BaseClass(GXTEXTURE_TYPE_CUBE)
{
}
CResourceTextureCube::~CResourceTextureCube()
{
mem_delete_a(pDataBlob);
}
UINT XMETHODCALLTYPE CResourceTextureCube::getSize() const
{
return(m_uSize);
}
void XMETHODCALLTYPE CResourceTextureCube::init(UINT uSize, GXFORMAT format, UINT uMipmapCount, UINT uFrameCount)
{
assert(uSize);
assert(uMipmapCount > 0);
m_format = format;
m_uFrameCount = uFrameCount;
m_uMipmapCount = uMipmapCount;
m_uSize = uSize;
mem_delete_a(pDataBlob);
if(uMipmapCount == IXRESOURCE_TEXTURE_AUTO_MIPS)
{
UINT uTmpSize = uSize;
uMipmapCount = 1;
while((uTmpSize >>= 1) >= 4)
{
++uMipmapCount;
}
}
size_t sizeData = uFrameCount * (sizeof(void*) + uMipmapCount * sizeof(XImageMip));
size_t sizeFrame = getTextureBytes(format, uSize, uSize) * 6;
size_t sizeTmp = sizeFrame;
for(UINT i = 0; i < uMipmapCount; ++i)
{
sizeData += sizeTmp * uFrameCount;
sizeTmp >>= 2;
}
pDataBlob = new byte[sizeData];
pppData = (XImageMip**)pDataBlob;
byte *pTmp = pDataBlob + sizeof(void*) * uFrameCount;
for(UINT i = 0; i < uFrameCount; ++i)
{
pppData[i] = (XImageMip*)pTmp;
pTmp += sizeof(void*) * uMipmapCount;
}
for(UINT i = 0; i < uFrameCount; ++i)
{
sizeTmp = sizeFrame;
for(UINT j = 0; i < uMipmapCount; ++i)
{
pppData[i][j].pData = pTmp;
pppData[i][j].sizeData = sizeTmp;
pppData[i][j].isWritten = false;
pTmp += sizeTmp;
sizeTmp >>= 2;
}
}
}
const IXResourceTextureCube* XMETHODCALLTYPE CResourceTextureCube::asCube() const
{
return(this);
}
IXResourceTextureCube* XMETHODCALLTYPE CResourceTextureCube::asCube()
{
return(this);
}
#ifndef __RESOURCETEXTURE_H
#define __RESOURCETEXTURE_H
#include <xcommon/resource/IXResourceTexture.h>
template<class T>
class CResourceTextureImpl: public T
{
public:
typedef CResourceTextureImpl<T> BaseClass;
CResourceTextureImpl(GXTEXTURE_TYPE type):
m_type(type)
{
}
GXTEXTURE_TYPE XMETHODCALLTYPE getType() const override
{
return(m_type);
}
GXFORMAT XMETHODCALLTYPE getFormat() const override
{
return(m_format);
}
UINT XMETHODCALLTYPE getFrameCount() const override
{
return(m_uFrameCount);
}
float XMETHODCALLTYPE getFrameTime() const override
{
return(m_fFrameTime);
}
void XMETHODCALLTYPE setFrameTime(float fTime) override
{
m_fFrameTime = fTime;
}
UINT XMETHODCALLTYPE getMipmapCount() const override
{
return(m_uMipmapCount);
}
XImageMip* XMETHODCALLTYPE getMip(UINT uMipmap, UINT uFrame = 0) override
{
assert(pppData);
assert(m_uMipmapCount < uMipmap);
assert(m_uFrameCount < uFrame);
if(!pppData || m_uMipmapCount >= uMipmap || m_uFrameCount >= uFrame)
{
return(NULL);
}
return(&pppData[uFrame][uMipmap]);
}
const XImageMip* XMETHODCALLTYPE getMip(UINT uMipmap, UINT uFrame = 0) const override
{
assert(pppData);
assert(m_uMipmapCount < uMipmap);
assert(m_uFrameCount < uFrame);
if(!pppData || m_uMipmapCount >= uMipmap || m_uFrameCount >= uFrame)
{
return(NULL);
}
return(&pppData[uFrame][uMipmap]);
}
const IXResourceTexture2D* XMETHODCALLTYPE as2D() const override
{
return(NULL);
}
const IXResourceTextureCube* XMETHODCALLTYPE asCube() const override
{
return(NULL);
}
IXResourceTexture2D* XMETHODCALLTYPE as2D() override
{
return(NULL);
}
IXResourceTextureCube* XMETHODCALLTYPE asCube() override
{
return(NULL);
}
void XMETHODCALLTYPE makeReadOnly() override
{
//! @todo Implement me!
}
UINT getBitsPerPixel(GXFORMAT format)
{
switch(format)
{
case GXFMT_A32B32G32R32F:
return(128);
case GXFMT_G32R32F:
case GXFMT_A16B16G16R16F:
case GXFMT_A16B16G16R16:
return(64);
case GXFMT_R32F:
case GXFMT_G16R16F:
case GXFMT_D24X8:
case GXFMT_D24S8:
case GXFMT_D32:
case GXFMT_X8R8G8B8:
case GXFMT_A8R8G8B8:
return(32);
case GXFMT_R8G8B8:
return(24);
case GXFMT_D16:
case GXFMT_R16F:
case GXFMT_R5G6B5:
return(16);
case GXFMT_DXT1:
case GXFMT_ATI1N:
return(4);
case GXFMT_DXT3:
case GXFMT_DXT5:
case GXFMT_ATI2N:
return(8);
}
assert(!"Unknown format!");
return(0);
}
UINT XMETHODCALLTYPE getTextureBytes(GXFORMAT format, UINT uWidth, UINT uHeight) override
{
bool bc = true;
int bcnumBytesPerBlock = 16;
switch(format)
{
case GXFMT_DXT1:
bc = true;
bcnumBytesPerBlock = 8;
break;
case GXFMT_DXT3:
bc = true;
break;
case GXFMT_DXT5:
bc = true;
break;
case GXFMT_ATI1N:
bc = true;
bcnumBytesPerBlock = 8;
break;
case GXFMT_ATI2N:
bc = true;
break;
default:
bc = false;
}
if(bc)
{
int numBlocksWide = max(1, uWidth / 4);
int numBlocksHigh = max(1, uHeight / 4);
return(numBlocksWide * numBlocksHigh * bcnumBytesPerBlock);
}
else
{
UINT bpp = getBitsPerPixel(format);
return((uWidth * bpp + 7) / 8 * uHeight); // round up to nearest byte
}
}
protected:
GXTEXTURE_TYPE m_type = GXTEXTURE_TYPE_UNKNOWN;
GXFORMAT m_format = GXFMT_UNKNOWN;
UINT m_uFrameCount = 0;
UINT m_uMipmapCount = 0;
float m_fFrameTime = 0.0f;
byte *pDataBlob = NULL;
XImageMip **pppData = NULL; // pppData[uFrame][uMip]
};
// Implemented in core
class CResourceTexture2D: public CResourceTextureImpl<IXResourceTexture2D>
{
public:
CResourceTexture2D();
~CResourceTexture2D();
UINT XMETHODCALLTYPE getWidth() const override;
UINT XMETHODCALLTYPE getHeight() const override;
void XMETHODCALLTYPE init(UINT uWidth, UINT uHeight, GXFORMAT format, UINT uMipmapCount = IXRESOURCE_TEXTURE_AUTO_MIPS, UINT uFrameCount = 0) override;
const IXResourceTexture2D* XMETHODCALLTYPE as2D() const override;
IXResourceTexture2D* XMETHODCALLTYPE as2D() override;
protected:
UINT m_uWidth = 0;
UINT m_uHeight = 0;
};
// Implemented in core
class CResourceTextureCube: public CResourceTextureImpl<IXResourceTextureCube>
{
public:
CResourceTextureCube();
~CResourceTextureCube();
UINT XMETHODCALLTYPE getSize() const override;
void XMETHODCALLTYPE init(UINT uSize, GXFORMAT format, UINT uMipmapCount = IXRESOURCE_TEXTURE_AUTO_MIPS, UINT uFrameCount = 0) override;
const IXResourceTextureCube* XMETHODCALLTYPE asCube() const override;
IXResourceTextureCube* XMETHODCALLTYPE asCube() override;
protected:
UINT m_uSize = 0;
};
#endif
#ifndef __IXRESOURCETEXTURE_H
#define __IXRESOURCETEXTURE_H
#include <gdefines.h>
#include <graphix/graphix.h>
#define IXRESOURCE_TEXTURE_AUTO_MIPS ~((UINT)0)
struct XImageMip
{
byte *pData;
size_t sizeData;
bool isWritten = false;
};
// Implemented in core
class IXResourceTexture2D;
class IXResourceTextureCube;
class IXResourceTexture: public IXUnknown
{
public:
virtual GXTEXTURE_TYPE XMETHODCALLTYPE getType() const = 0;
virtual GXFORMAT XMETHODCALLTYPE getFormat() const = 0;
virtual UINT XMETHODCALLTYPE getFrameCount() const = 0;
virtual float XMETHODCALLTYPE getFrameTime() const = 0;
virtual void XMETHODCALLTYPE setFrameTime(float fTime) = 0;
virtual UINT XMETHODCALLTYPE getMipmapCount() const = 0;
virtual XImageMip* XMETHODCALLTYPE getMip(UINT uMipmap, UINT uFrame = 0) = 0;
virtual const XImageMip* XMETHODCALLTYPE getMip(UINT uMipmap, UINT uFrame = 0) const = 0;
virtual const IXResourceTexture2D* XMETHODCALLTYPE as2D() const = 0;
virtual const IXResourceTextureCube* XMETHODCALLTYPE asCube() const = 0;
virtual IXResourceTexture2D* XMETHODCALLTYPE as2D() = 0;
virtual IXResourceTextureCube* XMETHODCALLTYPE asCube() = 0;
virtual void XMETHODCALLTYPE makeReadOnly() = 0;
virtual UINT XMETHODCALLTYPE getTextureBytes(GXFORMAT format, UINT uWidth, UINT uHeight) = 0;
};
// Implemented in core
class IXResourceTexture2D: public IXResourceTexture
{
public:
virtual UINT XMETHODCALLTYPE getWidth() const = 0;
virtual UINT XMETHODCALLTYPE getHeight() const = 0;
virtual void XMETHODCALLTYPE init(UINT uWidth, UINT uHeight, GXFORMAT format, UINT uMipmapCount = IXRESOURCE_TEXTURE_AUTO_MIPS, UINT uFrameCount = 0) = 0;
};
// Implemented in core
class IXResourceTextureCube: public IXResourceTexture
{
public:
virtual UINT XMETHODCALLTYPE getSize() const = 0;
virtual void XMETHODCALLTYPE init(UINT uSize, GXFORMAT format, UINT uMipmapCount = IXRESOURCE_TEXTURE_AUTO_MIPS, UINT uFrameCount = 0) = 0;
};
#endif
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