Refectoring in preparation for non-compressed texture support

--HG--
extra : convert_revision : svn%3A7752d6cf-9f14-4ad2-affc-04f1e67b81a5/trunk%40138
This commit is contained in:
kearwood
2012-10-24 06:57:34 +00:00
parent fa91aafb5b
commit 29b6004b2e
8 changed files with 301 additions and 185 deletions

View File

@@ -146,6 +146,14 @@
E4BBBB9E1512A4AE00F43B5B /* AppKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = E4BBBB961512A46700F43B5B /* AppKit.framework */; }; E4BBBB9E1512A4AE00F43B5B /* AppKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = E4BBBB961512A46700F43B5B /* AppKit.framework */; };
E4BBBB9F1512A4B100F43B5B /* Cocoa.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = E4BBBB941512A45500F43B5B /* Cocoa.framework */; }; E4BBBB9F1512A4B100F43B5B /* Cocoa.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = E4BBBB941512A45500F43B5B /* Cocoa.framework */; };
E4BBBBA71512A6DC00F43B5B /* KRVector3.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E491017F13C99BDC0098455B /* KRVector3.cpp */; }; E4BBBBA71512A6DC00F43B5B /* KRVector3.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E491017F13C99BDC0098455B /* KRVector3.cpp */; };
E4CA10E51637BD0A005D9400 /* KRTexturePVR.h in Headers */ = {isa = PBXBuildFile; fileRef = E4CA10E41637BD0A005D9400 /* KRTexturePVR.h */; };
E4CA10E61637BD0A005D9400 /* KRTexturePVR.h in Headers */ = {isa = PBXBuildFile; fileRef = E4CA10E41637BD0A005D9400 /* KRTexturePVR.h */; };
E4CA10E91637BD2B005D9400 /* KRTexturePVR.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E4CA10E81637BD2B005D9400 /* KRTexturePVR.cpp */; };
E4CA10EA1637BD2B005D9400 /* KRTexturePVR.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E4CA10E81637BD2B005D9400 /* KRTexturePVR.cpp */; };
E4CA10EC1637BD47005D9400 /* KRTextureTGA.h in Headers */ = {isa = PBXBuildFile; fileRef = E4CA10EB1637BD47005D9400 /* KRTextureTGA.h */; };
E4CA10ED1637BD47005D9400 /* KRTextureTGA.h in Headers */ = {isa = PBXBuildFile; fileRef = E4CA10EB1637BD47005D9400 /* KRTextureTGA.h */; };
E4CA10EF1637BD58005D9400 /* KRTextureTGA.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E4CA10EE1637BD58005D9400 /* KRTextureTGA.cpp */; };
E4CA10F01637BD58005D9400 /* KRTextureTGA.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E4CA10EE1637BD58005D9400 /* KRTextureTGA.cpp */; };
E4CE184D15FF124600F80870 /* light_point_inside.fsh in Sources */ = {isa = PBXBuildFile; fileRef = E4CE184C15FF124600F80870 /* light_point_inside.fsh */; }; E4CE184D15FF124600F80870 /* light_point_inside.fsh in Sources */ = {isa = PBXBuildFile; fileRef = E4CE184C15FF124600F80870 /* light_point_inside.fsh */; };
E4CE184F15FF125700F80870 /* light_point_inside.vsh in Sources */ = {isa = PBXBuildFile; fileRef = E4CE184E15FF125700F80870 /* light_point_inside.vsh */; }; E4CE184F15FF125700F80870 /* light_point_inside.vsh in Sources */ = {isa = PBXBuildFile; fileRef = E4CE184E15FF125700F80870 /* light_point_inside.vsh */; };
E4D0683F1512A790005FFBEB /* KRVector3.h in Headers */ = {isa = PBXBuildFile; fileRef = E491017E13C99BDC0098455B /* KRVector3.h */; settings = {ATTRIBUTES = (Public, ); }; }; E4D0683F1512A790005FFBEB /* KRVector3.h in Headers */ = {isa = PBXBuildFile; fileRef = E491017E13C99BDC0098455B /* KRVector3.h */; settings = {ATTRIBUTES = (Public, ); }; };
@@ -301,6 +309,10 @@
E4BBBB961512A46700F43B5B /* AppKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AppKit.framework; path = ../../../../MacOSX.platform/Developer/SDKs/MacOSX10.7.sdk/System/Library/Frameworks/AppKit.framework; sourceTree = SDKROOT; }; E4BBBB961512A46700F43B5B /* AppKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AppKit.framework; path = ../../../../MacOSX.platform/Developer/SDKs/MacOSX10.7.sdk/System/Library/Frameworks/AppKit.framework; sourceTree = SDKROOT; };
E4BBBB981512A47500F43B5B /* CoreData.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreData.framework; path = ../../../../MacOSX.platform/Developer/SDKs/MacOSX10.7.sdk/System/Library/Frameworks/CoreData.framework; sourceTree = SDKROOT; }; E4BBBB981512A47500F43B5B /* CoreData.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreData.framework; path = ../../../../MacOSX.platform/Developer/SDKs/MacOSX10.7.sdk/System/Library/Frameworks/CoreData.framework; sourceTree = SDKROOT; };
E4BBBB9A1512A48200F43B5B /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = ../../../../MacOSX.platform/Developer/SDKs/MacOSX10.7.sdk/System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; }; E4BBBB9A1512A48200F43B5B /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = ../../../../MacOSX.platform/Developer/SDKs/MacOSX10.7.sdk/System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; };
E4CA10E41637BD0A005D9400 /* KRTexturePVR.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = KRTexturePVR.h; path = Classes/KRTexturePVR.h; sourceTree = "<group>"; };
E4CA10E81637BD2B005D9400 /* KRTexturePVR.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = KRTexturePVR.cpp; path = Classes/KRTexturePVR.cpp; sourceTree = "<group>"; };
E4CA10EB1637BD47005D9400 /* KRTextureTGA.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = KRTextureTGA.h; path = Classes/KRTextureTGA.h; sourceTree = "<group>"; };
E4CA10EE1637BD58005D9400 /* KRTextureTGA.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = KRTextureTGA.cpp; path = Classes/KRTextureTGA.cpp; sourceTree = "<group>"; };
E4CE184815FEEDA200F80870 /* font.pvr */ = {isa = PBXFileReference; lastKnownFileType = file; name = font.pvr; path = Shaders/font.pvr; sourceTree = "<group>"; }; E4CE184815FEEDA200F80870 /* font.pvr */ = {isa = PBXFileReference; lastKnownFileType = file; name = font.pvr; path = Shaders/font.pvr; sourceTree = "<group>"; };
E4CE184C15FF124600F80870 /* light_point_inside.fsh */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.glsl; lineEnding = 0; name = light_point_inside.fsh; path = Shaders/light_point_inside.fsh; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.glsl; }; E4CE184C15FF124600F80870 /* light_point_inside.fsh */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.glsl; lineEnding = 0; name = light_point_inside.fsh; path = Shaders/light_point_inside.fsh; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.glsl; };
E4CE184E15FF125700F80870 /* light_point_inside.vsh */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.glsl; name = light_point_inside.vsh; path = Shaders/light_point_inside.vsh; sourceTree = "<group>"; }; E4CE184E15FF125700F80870 /* light_point_inside.vsh */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.glsl; name = light_point_inside.vsh; path = Shaders/light_point_inside.vsh; sourceTree = "<group>"; };
@@ -470,6 +482,10 @@
E4B175AB161F5A1000B8FB80 /* KRTexture.h */, E4B175AB161F5A1000B8FB80 /* KRTexture.h */,
E4B175B0161F5FAE00B8FB80 /* KRTextureCube.cpp */, E4B175B0161F5FAE00B8FB80 /* KRTextureCube.cpp */,
E4B175B1161F5FAF00B8FB80 /* KRTextureCube.h */, E4B175B1161F5FAF00B8FB80 /* KRTextureCube.h */,
E4CA10E41637BD0A005D9400 /* KRTexturePVR.h */,
E4CA10E81637BD2B005D9400 /* KRTexturePVR.cpp */,
E4CA10EB1637BD47005D9400 /* KRTextureTGA.h */,
E4CA10EE1637BD58005D9400 /* KRTextureTGA.cpp */,
); );
name = Texture; name = Texture;
sourceTree = "<group>"; sourceTree = "<group>";
@@ -689,6 +705,8 @@
E4030E4C160A3CF000592648 /* KRStockGeometry.h in Headers */, E4030E4C160A3CF000592648 /* KRStockGeometry.h in Headers */,
E4B175AE161F5A1000B8FB80 /* KRTexture.h in Headers */, E4B175AE161F5A1000B8FB80 /* KRTexture.h in Headers */,
E4B175B4161F5FAF00B8FB80 /* KRTextureCube.h in Headers */, E4B175B4161F5FAF00B8FB80 /* KRTextureCube.h in Headers */,
E4CA10E51637BD0A005D9400 /* KRTexturePVR.h in Headers */,
E4CA10EC1637BD47005D9400 /* KRTextureTGA.h in Headers */,
); );
runOnlyForDeploymentPostprocessing = 0; runOnlyForDeploymentPostprocessing = 0;
}; };
@@ -738,6 +756,8 @@
E4030E4D160A3CF000592648 /* KRStockGeometry.h in Headers */, E4030E4D160A3CF000592648 /* KRStockGeometry.h in Headers */,
E4B175AF161F5A1000B8FB80 /* KRTexture.h in Headers */, E4B175AF161F5A1000B8FB80 /* KRTexture.h in Headers */,
E4B175B5161F5FAF00B8FB80 /* KRTextureCube.h in Headers */, E4B175B5161F5FAF00B8FB80 /* KRTextureCube.h in Headers */,
E4CA10E61637BD0A005D9400 /* KRTexturePVR.h in Headers */,
E4CA10ED1637BD47005D9400 /* KRTextureTGA.h in Headers */,
); );
runOnlyForDeploymentPostprocessing = 0; runOnlyForDeploymentPostprocessing = 0;
}; };
@@ -867,6 +887,8 @@
E4B175B2161F5FAF00B8FB80 /* KRTextureCube.cpp in Sources */, E4B175B2161F5FAF00B8FB80 /* KRTextureCube.cpp in Sources */,
E41B841E16260C5600C7A771 /* sky_box.fsh in Sources */, E41B841E16260C5600C7A771 /* sky_box.fsh in Sources */,
E41B842116260C6500C7A771 /* sky_box.vsh in Sources */, E41B842116260C6500C7A771 /* sky_box.vsh in Sources */,
E4CA10E91637BD2B005D9400 /* KRTexturePVR.cpp in Sources */,
E4CA10EF1637BD58005D9400 /* KRTextureTGA.cpp in Sources */,
); );
runOnlyForDeploymentPostprocessing = 0; runOnlyForDeploymentPostprocessing = 0;
}; };
@@ -912,6 +934,8 @@
E488399D15F92BE000BD66D5 /* KRBundleManager.cpp in Sources */, E488399D15F92BE000BD66D5 /* KRBundleManager.cpp in Sources */,
E4B175AD161F5A1000B8FB80 /* KRTexture.cpp in Sources */, E4B175AD161F5A1000B8FB80 /* KRTexture.cpp in Sources */,
E4B175B3161F5FAF00B8FB80 /* KRTextureCube.cpp in Sources */, E4B175B3161F5FAF00B8FB80 /* KRTextureCube.cpp in Sources */,
E4CA10EA1637BD2B005D9400 /* KRTexturePVR.cpp in Sources */,
E4CA10F01637BD58005D9400 /* KRTextureTGA.cpp in Sources */,
); );
runOnlyForDeploymentPostprocessing = 0; runOnlyForDeploymentPostprocessing = 0;
}; };

View File

@@ -41,136 +41,14 @@
#import <stdint.h> #import <stdint.h>
#import <assert.h> #import <assert.h>
#define PVR_TEXTURE_FLAG_TYPE_MASK 0xff
static char gPVRTexIdentifier[5] = "PVR!";
enum
{
kPVRTextureFlagTypePVRTC_2 = 24,
kPVRTextureFlagTypePVRTC_4
};
typedef struct _PVRTexHeader
{
uint32_t headerLength;
uint32_t height;
uint32_t width;
uint32_t numMipmaps;
uint32_t flags;
uint32_t dataLength;
uint32_t bpp;
uint32_t bitmaskRed;
uint32_t bitmaskGreen;
uint32_t bitmaskBlue;
uint32_t bitmaskAlpha;
uint32_t pvrTag;
uint32_t numSurfs;
} PVRTexHeader;
KRTexture2D::KRTexture2D(KRContext &context, KRDataBlock *data) : KRTexture(context) { KRTexture2D::KRTexture2D(KRContext &context, KRDataBlock *data) : KRTexture(context) {
m_pData = data; m_pData = data;
m_current_lod_max_dim = 0;
load();
} }
KRTexture2D::~KRTexture2D() { KRTexture2D::~KRTexture2D() {
delete m_pData; delete m_pData;
} }
bool KRTexture2D::hasMipmaps() {
return m_blocks.size() > 1;
}
int KRTexture2D::getMaxMipMap() {
return m_max_lod_max_dim;
}
int KRTexture2D::getMinMipMap() {
return m_min_lod_max_dim;
}
bool KRTexture2D::load() {
#if TARGET_OS_IPHONE
PVRTexHeader *header = (PVRTexHeader *)m_pData->getStart();
uint32_t formatFlags = header->flags & PVR_TEXTURE_FLAG_TYPE_MASK;
if (formatFlags == kPVRTextureFlagTypePVRTC_4) {
m_internalFormat = GL_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG;
} else if(formatFlags == kPVRTextureFlagTypePVRTC_2) {
m_internalFormat = GL_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG;
} else {
assert(false);
}
uint32_t pvrTag = header->pvrTag;
if (gPVRTexIdentifier[0] != ((pvrTag >> 0) & 0xff) ||
gPVRTexIdentifier[1] != ((pvrTag >> 8) & 0xff) ||
gPVRTexIdentifier[2] != ((pvrTag >> 16) & 0xff) ||
gPVRTexIdentifier[3] != ((pvrTag >> 24) & 0xff))
{
return false;
}
m_iWidth = header->width; // Note: call __builtin_bswap32 when needed to switch endianness
m_iHeight = header->height;
m_bHasAlpha = header->bitmaskAlpha;
uint8_t *bytes = ((uint8_t *)m_pData->getStart()) + sizeof(PVRTexHeader);
uint32_t dataLength = header->dataLength, dataOffset = 0, dataSize = 0;
uint32_t width = m_iWidth, height = m_iHeight, bpp = 4;
uint32_t blockSize = 0, widthBlocks = 0, heightBlocks = 0;
// Calculate the data size for each texture level and respect the minimum number of blocks
while(dataOffset < dataLength) {
if (formatFlags == kPVRTextureFlagTypePVRTC_4) {
blockSize = 4 * 4; // Pixel by pixel block size for 4bpp
widthBlocks = width / 4;
heightBlocks = height / 4;
bpp = 4;
} else {
blockSize = 8 * 4; // Pixel by pixel block size for 2bpp
widthBlocks = width / 8;
heightBlocks = height / 4;
bpp = 2;
}
// Clamp to minimum number of blocks
if (widthBlocks < 2) {
widthBlocks = 2;
}
if (heightBlocks < 2) {
heightBlocks = 2;
}
dataSize = widthBlocks * heightBlocks * ((blockSize * bpp) / 8);
dataBlockStruct newBlock;
newBlock.start = bytes+dataOffset;
newBlock.length = dataSize;
m_blocks.push_back(newBlock);
dataOffset += dataSize;
width = width >> 1;
if(width < 1) {
width = 1;
}
height = height >> 1;
if(height < 1) {
height = 1;
}
}
m_max_lod_max_dim = m_iWidth > m_iHeight ? m_iWidth : m_iHeight;
m_min_lod_max_dim = width >> height ? width : height;
#endif
return true;
}
bool KRTexture2D::createGLTexture(int lod_max_dim) { bool KRTexture2D::createGLTexture(int lod_max_dim) {
m_current_lod_max_dim = 0; m_current_lod_max_dim = 0;
GLDEBUG(glGenTextures(1, &m_iHandle)); GLDEBUG(glGenTextures(1, &m_iHandle));
@@ -193,49 +71,6 @@ bool KRTexture2D::createGLTexture(int lod_max_dim) {
return true; return true;
} }
bool KRTexture2D::uploadTexture(GLenum target, int lod_max_dim, int &current_lod_max_dim, size_t &textureMemUsed)
{
int width = m_iWidth;
int height = m_iHeight;
GLenum err;
if(m_blocks.size() == 0) {
return false;
}
int i=0;
for(std::list<dataBlockStruct>::iterator itr = m_blocks.begin(); itr != m_blocks.end(); itr++) {
dataBlockStruct block = *itr;
if(width <= lod_max_dim && height <= lod_max_dim) {
if(width > current_lod_max_dim) {
current_lod_max_dim = width;
}
if(height > current_lod_max_dim) {
current_lod_max_dim = height;
}
glCompressedTexImage2D(target, i, m_internalFormat, width, height, 0, block.length, block.start);
err = glGetError();
if (err != GL_NO_ERROR) {
return false;
}
textureMemUsed += block.length;
i++;
}
width = width >> 1;
if(width < 1) {
width = 1;
}
height = height >> 1;
if(height < 1) {
height = 1;
}
}
return true;
}
void KRTexture2D::bind(size_t &textureMemUsed, int max_dim, bool can_resize) { void KRTexture2D::bind(size_t &textureMemUsed, int max_dim, bool can_resize) {
textureMemUsed -= getMemSize(); textureMemUsed -= getMemSize();
GLDEBUG(glBindTexture(GL_TEXTURE_2D, getHandle(max_dim, can_resize))); GLDEBUG(glBindTexture(GL_TEXTURE_2D, getHandle(max_dim, can_resize)));
@@ -248,3 +83,15 @@ void KRTexture2D::bind(size_t &textureMemUsed, int max_dim, bool can_resize) {
textureMemUsed += getMemSize(); textureMemUsed += getMemSize();
} }
int KRTexture2D::getMaxMipMap() {
return m_max_lod_max_dim;
}
int KRTexture2D::getMinMipMap() {
return m_min_lod_max_dim;
}
bool KRTexture2D::hasMipmaps() {
return m_max_lod_max_dim == m_min_lod_max_dim;
}

View File

@@ -46,35 +46,19 @@ using std::list;
class KRTexture2D : public KRTexture { class KRTexture2D : public KRTexture {
public: public:
KRTexture2D(KRContext &context, KRDataBlock *data); KRTexture2D(KRContext &context, KRDataBlock *data);
~KRTexture2D(); virtual ~KRTexture2D();
bool hasMipmaps(); bool hasMipmaps();
int getMaxMipMap(); int getMaxMipMap();
int getMinMipMap(); int getMinMipMap();
bool uploadTexture(GLenum target, int lod_max_dim, int &current_lod_max_dim, size_t &textureMemUsed); virtual bool uploadTexture(GLenum target, int lod_max_dim, int &current_lod_max_dim, size_t &textureMemUsed) = 0;
virtual void bind(size_t &textureMemUsed, int max_dim, bool can_resize); virtual void bind(size_t &textureMemUsed, int max_dim, bool can_resize);
private: protected:
KRDataBlock *m_pData; KRDataBlock *m_pData;
virtual bool createGLTexture(int lod_max_dim); virtual bool createGLTexture(int lod_max_dim);
uint32_t m_iWidth;
uint32_t m_iHeight;
GLenum m_internalFormat;
bool m_bHasAlpha;
struct dataBlockStruct {
void *start;
uint32_t length;
};
std::list<dataBlockStruct> m_blocks;
bool load();
}; };
#endif #endif

View File

@@ -32,6 +32,8 @@
#include "KRTextureManager.h" #include "KRTextureManager.h"
#include "KRContext.h" #include "KRContext.h"
#include "KRTexture2D.h" #include "KRTexture2D.h"
#include "KRTexturePVR.h"
#include "KRTextureTGA.h"
#include "KRTextureCube.h" #include "KRTextureCube.h"
#include "KRContext.h" #include "KRContext.h"
#include <string.h> #include <string.h>
@@ -52,7 +54,7 @@ KRTextureManager::~KRTextureManager() {
} }
KRTexture *KRTextureManager::loadTexture(const char *szName, KRDataBlock *data) { KRTexture *KRTextureManager::loadTexture(const char *szName, KRDataBlock *data) {
KRTexture *pTexture = new KRTexture2D(getContext(), data); KRTexture *pTexture = new KRTexturePVR(getContext(), data);
std::string lowerName = szName; std::string lowerName = szName;
std::transform(lowerName.begin(), lowerName.end(), std::transform(lowerName.begin(), lowerName.end(),

View File

@@ -0,0 +1,194 @@
//
// KRTexturePVR.cpp
// KREngine
//
// Copyright 2012 Kearwood Gilbert. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are
// permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of
// conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
// of conditions and the following disclaimer in the documentation and/or other materials
// provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY KEARWOOD GILBERT ''AS IS'' AND ANY EXPRESS OR IMPLIED
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KEARWOOD GILBERT OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
// ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// The views and conclusions contained in the software and documentation are those of the
// authors and should not be interpreted as representing official policies, either expressed
// or implied, of Kearwood Gilbert.
//
#include "KRTexturePVR.h"
#include "KRTextureManager.h"
#import <stdint.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#import <stdint.h>
#import <assert.h>
#import "KREngine-common.h"
#define PVR_TEXTURE_FLAG_TYPE_MASK 0xff
static char gPVRTexIdentifier[5] = "PVR!";
enum
{
kPVRTextureFlagTypePVRTC_2 = 24,
kPVRTextureFlagTypePVRTC_4
};
typedef struct _PVRTexHeader
{
uint32_t headerLength;
uint32_t height;
uint32_t width;
uint32_t numMipmaps;
uint32_t flags;
uint32_t dataLength;
uint32_t bpp;
uint32_t bitmaskRed;
uint32_t bitmaskGreen;
uint32_t bitmaskBlue;
uint32_t bitmaskAlpha;
uint32_t pvrTag;
uint32_t numSurfs;
} PVRTexHeader;
KRTexturePVR::KRTexturePVR(KRContext &context, KRDataBlock *data) : KRTexture2D(context, data) {
#if TARGET_OS_IPHONE
PVRTexHeader *header = (PVRTexHeader *)m_pData->getStart();
uint32_t formatFlags = header->flags & PVR_TEXTURE_FLAG_TYPE_MASK;
if (formatFlags == kPVRTextureFlagTypePVRTC_4) {
m_internalFormat = GL_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG;
} else if(formatFlags == kPVRTextureFlagTypePVRTC_2) {
m_internalFormat = GL_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG;
} else {
assert(false);
}
uint32_t pvrTag = header->pvrTag;
if (gPVRTexIdentifier[0] != ((pvrTag >> 0) & 0xff) ||
gPVRTexIdentifier[1] != ((pvrTag >> 8) & 0xff) ||
gPVRTexIdentifier[2] != ((pvrTag >> 16) & 0xff) ||
gPVRTexIdentifier[3] != ((pvrTag >> 24) & 0xff)) {
assert(false);
}
m_iWidth = header->width; // Note: call __builtin_bswap32 when needed to switch endianness
m_iHeight = header->height;
m_bHasAlpha = header->bitmaskAlpha;
uint8_t *bytes = ((uint8_t *)m_pData->getStart()) + sizeof(PVRTexHeader);
uint32_t dataLength = header->dataLength, dataOffset = 0, dataSize = 0;
uint32_t width = m_iWidth, height = m_iHeight, bpp = 4;
uint32_t blockSize = 0, widthBlocks = 0, heightBlocks = 0;
// Calculate the data size for each texture level and respect the minimum number of blocks
while(dataOffset < dataLength) {
if (formatFlags == kPVRTextureFlagTypePVRTC_4) {
blockSize = 4 * 4; // Pixel by pixel block size for 4bpp
widthBlocks = width / 4;
heightBlocks = height / 4;
bpp = 4;
} else {
blockSize = 8 * 4; // Pixel by pixel block size for 2bpp
widthBlocks = width / 8;
heightBlocks = height / 4;
bpp = 2;
}
// Clamp to minimum number of blocks
if (widthBlocks < 2) {
widthBlocks = 2;
}
if (heightBlocks < 2) {
heightBlocks = 2;
}
dataSize = widthBlocks * heightBlocks * ((blockSize * bpp) / 8);
dataBlockStruct newBlock;
newBlock.start = bytes+dataOffset;
newBlock.length = dataSize;
m_blocks.push_back(newBlock);
dataOffset += dataSize;
width = width >> 1;
if(width < 1) {
width = 1;
}
height = height >> 1;
if(height < 1) {
height = 1;
}
}
m_max_lod_max_dim = m_iWidth > m_iHeight ? m_iWidth : m_iHeight;
m_min_lod_max_dim = width > height ? width : height;
#endif
}
KRTexturePVR::~KRTexturePVR() {
}
bool KRTexturePVR::uploadTexture(GLenum target, int lod_max_dim, int &current_lod_max_dim, size_t &textureMemUsed)
{
int width = m_iWidth;
int height = m_iHeight;
GLenum err;
if(m_blocks.size() == 0) {
return false;
}
int i=0;
for(std::list<dataBlockStruct>::iterator itr = m_blocks.begin(); itr != m_blocks.end(); itr++) {
dataBlockStruct block = *itr;
if(width <= lod_max_dim && height <= lod_max_dim) {
if(width > current_lod_max_dim) {
current_lod_max_dim = width;
}
if(height > current_lod_max_dim) {
current_lod_max_dim = height;
}
glCompressedTexImage2D(target, i, m_internalFormat, width, height, 0, block.length, block.start);
err = glGetError();
if (err != GL_NO_ERROR) {
return false;
}
textureMemUsed += block.length;
i++;
}
width = width >> 1;
if(width < 1) {
width = 1;
}
height = height >> 1;
if(height < 1) {
height = 1;
}
}
return true;
}

View File

@@ -0,0 +1,37 @@
//
// KRTexturePVR.h
// KREngine
//
// Created by Kearwood Gilbert on 2012-10-23.
// Copyright (c) 2012 Kearwood Software. All rights reserved.
//
#ifndef KRTEXTUREPVR_H
#define KRTEXTUREPVR_H
#include "KRTexture2D.h"
class KRTexturePVR : public KRTexture2D
{
public:
KRTexturePVR(KRContext &context, KRDataBlock *data);
virtual ~KRTexturePVR();
bool uploadTexture(GLenum target, int lod_max_dim, int &current_lod_max_dim, size_t &textureMemUsed);
protected:
uint32_t m_iWidth;
uint32_t m_iHeight;
GLenum m_internalFormat;
bool m_bHasAlpha;
struct dataBlockStruct {
void *start;
uint32_t length;
};
std::list<dataBlockStruct> m_blocks;
};
#endif

View File

@@ -0,0 +1,9 @@
//
// KRTextureTGA.cpp
// KREngine
//
// Created by Kearwood Gilbert on 2012-10-23.
// Copyright (c) 2012 Kearwood Software. All rights reserved.
//
#include "KRTextureTGA.h"

View File

@@ -0,0 +1,19 @@
//
// KRTextureTGA.h
// KREngine
//
// Created by Kearwood Gilbert on 2012-10-23.
// Copyright (c) 2012 Kearwood Software. All rights reserved.
//
#ifndef KRTEXTURETGA_H
#define KRTEXTURETGA_H
#include "KRTexture2D.h"
class KRTextureTGA : public KRTexture2D
{
};
#endif