38#ifdef OO_DEBUG_MONITOR_ENABLED
43#define RGBA_IMAGE_TYPE GL_UNSIGNED_INT_8_8_8_8_REV
44#elif OOLITE_LITTLE_ENDIAN
45#define RGBA_IMAGE_TYPE GL_UNSIGNED_BYTE
47#error Neither OOLITE_BIG_ENDIAN nor OOLITE_LITTLE_ENDIAN is defined as nonzero!
50@interface OOConcreteTexture (Private)
55#if OO_TEXTURE_CUBE_MAP
61#if OOTEXTURE_RELOADABLE
73 options:(uint32_t)options
74 anisotropy:(GLfloat)anisotropy
75 lodBias:(GLfloat)lodBias
89#if GL_EXT_texture_filter_anisotropic
92#if GL_EXT_texture_lod_bias
96#ifdef OO_DEBUG_MONITOR_ENABLED
109- (id)initWithPath:(NSString*)path
111 options:(uint32_t)options
112 anisotropy:(
float)anisotropy
113 lodBias:(GLfloat)lodBias
121 if ((
self = [
self initWithLoader:loader key:key options:options anisotropy:anisotropy lodBias:lodBias])) {
122#if OOTEXTURE_RELOADABLE
132#ifdef OO_DEBUG_MONITOR_ENABLED
133 OOLog(_trace ?
@"texture.allocTrace.dealloc" :
@"texture.dealloc",
@"Deallocating and uncaching texture %p",
self);
136#if OOTEXTURE_RELOADABLE
150#ifndef OOTEXTURE_NO_CACHE
158#ifdef OO_DEBUG_MONITOR_ENABLED
167 NSString* stateDesc =
nil;
171 stateDesc = [
NSString stringWithFormat:@"%u x %u", _width, _height];
173 stateDesc =
@"LOAD ERROR";
176 stateDesc =
@"loading";
179 return [
NSString stringWithFormat:@"%@, %@", _key, stateDesc];
187#ifdef OO_DEBUG_MONITOR_ENABLED
193#if OOTEXTURE_RELOADABLE
194 NSString* name = [_path lastPathComponent];
196 NSString* name = [[[[
self cacheKey] componentsSeparatedByString:@":"] objectAtIndex:0] lastPathComponent];
199 NSString* channelSuffix =
nil;
202 channelSuffix =
@":r";
206 channelSuffix =
@":g";
210 channelSuffix =
@":b";
214 channelSuffix =
@":a";
218 if (channelSuffix !=
nil)
219 name = [name stringByAppendingString:channelSuffix];
236#if GL_EXT_texture_lod_bias
238 OOGL(glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT, GL_TEXTURE_LOD_BIAS_EXT, _lodBias));
290#if OOTEXTURE_RELOADABLE
295 GLenum format, internalFormat, type;
306 glGetTexImage(GL_TEXTURE_2D, 0, format, type, px.
pixels);
308#if OO_TEXTURE_CUBE_MAP
313 uint8_t* pixels = px.
pixels;
316 for (i = 0; i < 6; i++) {
317 glGetTexImage(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, format, type, pixels);
330#if GL_EXT_texture_rectangle
331 return _isRectTexture;
339#if OO_TEXTURE_CUBE_MAP
348#if GL_EXT_texture_rectangle
350 if (!_isRectTexture) {
351 return NSMakeSize(1.0f, 1.0f);
358 return NSMakeSize(1.0f, 1.0f);
366 return NSMakeSize(1.0f, 1.0f);
379@implementation OOConcreteTexture (Private)
386 if ([_loader getResult:&pm format:&_format originalWidth:&_originalWidth originalHeight:&_originalHeight]) {
391#if OO_TEXTURE_CUBE_MAP
397#if defined(OO_DEBUG_MONITOR_ENABLED) && OOTEXTURE_RELOADABLE
399 static unsigned dumpID = 0;
400 NSString* name = [
NSString stringWithFormat:@"tex dump %u \"%@\"", ++dumpID, [
self name]];
401 OOLog(
@"texture.trace.dump",
@"Dumped traced texture %@ to \'%@.png\'",
self, name);
428 OOGL(glGenTextures(1, &_textureName));
429 OOGL(glBindTexture(texTarget, _textureName));
436#if OO_TEXTURE_CUBE_MAP
437 if (texTarget == GL_TEXTURE_CUBE_MAP) {
438 wrapS = wrapT = clampMode;
439 OOGL(glTexParameteri(texTarget, GL_TEXTURE_WRAP_R, clampMode));
443 OOGL(glTexParameteri(texTarget, GL_TEXTURE_WRAP_S, wrapS));
444 OOGL(glTexParameteri(texTarget, GL_TEXTURE_WRAP_T, wrapT));
452 filter = GL_LINEAR_MIPMAP_LINEAR;
455 OOGL(glTexParameteri(texTarget, GL_TEXTURE_MIN_FILTER, filter));
457#if GL_EXT_texture_filter_anisotropic
458 if (
gOOTextureInfo.anisotropyAvailable && mipMap && 1.0 < _anisotropy) {
459 OOGL(glTexParameterf(texTarget, GL_TEXTURE_MAX_ANISOTROPY_EXT, _anisotropy));
469 OOGL(glTexParameteri(texTarget, GL_TEXTURE_MAG_FILTER, filter));
473 if (texTarget == GL_TEXTURE_2D) {
475 OOLog(
@"texture.upload",
@"Uploaded texture %u (%ux%u pixels, %@)", _textureName, _width, _height, _key);
477#if OO_TEXTURE_CUBE_MAP
478 else if (texTarget == GL_TEXTURE_CUBE_MAP) {
479 [
self uploadTextureCubeMapDataWithMipMap:mipMap format:_format];
480 OOLog(
@"texture.upload",
@"Uploaded cube map texture %u (%ux%ux6 pixels, %@)", _textureName, _width, _width, _key);
484 [
NSException raise:NSInternalInconsistencyException format:@"Unhandled texture target 0x%X.", texTarget];
490#if OOTEXTURE_RELOADABLE
501 GLenum glFormat = 0, internalFormat = 0, type = 0;
505 char* bytes = _bytes;
510 if (!
DecodeFormat(format, _options, &glFormat, &internalFormat, &type))
513 while (0 < w && 0 < h) {
514 OOGL(glTexImage2D(GL_TEXTURE_2D, level++, internalFormat, w, h, 0, glFormat, type, bytes));
517 bytes += w * components * h;
523 _mipLevels = level - 1;
527#if OO_TEXTURE_CUBE_MAP
532 GLenum glFormat = 0, internalFormat = 0, type = 0;
533 if (!
DecodeFormat(format, _options, &glFormat, &internalFormat, &type))
538 size_t sideSize = _width * _width * components;
540 sideSize = sideSize * 4 / 3;
541 sideSize = (sideSize + 15) & ~15;
545 for (side = 0; side < 6; side++) {
546 char* bytes = _bytes;
547 bytes += side * sideSize;
549 unsigned w = _width, level = 0;
552 OOGL(glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + side, level++, internalFormat, w, w, 0, glFormat, type, bytes));
555 bytes += w * w * components;
564 GLenum texTarget = GL_TEXTURE_2D;
565#if OO_TEXTURE_CUBE_MAP
567 texTarget = GL_TEXTURE_CUBE_MAP;
575 if (_loaded && _uploaded && _valid) {
579 OOGL(glDeleteTextures(1, &_textureName));
582#if OOTEXTURE_RELOADABLE
584 OOLog(
@"texture.reload",
@"Reloading texture %@",
self);
598#if OOTEXTURE_RELOADABLE
611 NSCParameterAssert(outFormat != NULL && outInternalFormat != NULL && outType != NULL);
615 *outFormat = GL_RGBA;
616 *outInternalFormat = options &
kOOTextureSRGBA ? GL_SRGB_ALPHA : GL_RGBA;
617 *outType = RGBA_IMAGE_TYPE;
622 *outFormat = GL_ALPHA;
623 *outInternalFormat = GL_ALPHA8;
625 *outFormat = GL_LUMINANCE;
626 *outInternalFormat = GL_LUMINANCE8;
628 *outType = GL_UNSIGNED_BYTE;
632 *outFormat = GL_LUMINANCE_ALPHA;
633 *outInternalFormat = GL_LUMINANCE8_ALPHA8;
634 *outType = GL_UNSIGNED_BYTE;
static BOOL DecodeFormat(OOTextureDataFormat format, uint32_t options, GLenum *outFormat, GLenum *outInternalFormat, GLenum *outType)
OOINLINE float OOClamp_0_1_f(float value) INLINE_CONST_FUNC
#define OOLog(class, format,...)
NSString *const kOOLogParameterError
#define OO_ENTER_OPENGL()
void OODumpPixMap(OOPixMap pixMap, NSString *name)
unsigned short OOPixMapBytesPerPixelForFormat(OOPixMapFormat format) PURE_FUNC
const OOPixMap kOONullPixMap
OOPixMap OOAllocatePixMap(OOPixMapDimension width, OOPixMapDimension height, OOPixMapFormat format, size_t rowBytes, size_t bufferSize)
OOPixMap OOMakePixMap(void *pixels, OOPixMapDimension width, OOPixMapDimension height, OOPixMapFormat format, size_t rowBytes, size_t bufferSize)
BOOL OOIsValidPixMap(OOPixMap pixMap)
OOPixMap OODuplicatePixMap(OOPixMap srcPixMap, size_t desiredSize)
#define GL_TEXTURE_MAX_LEVEL
uint8_t OOTextureComponentsForFormat(OOTextureDataFormat format)
@ kOOTextureAllowRectTexture
@ kOOTextureExtractChannelA
@ kOOTextureExtractChannelB
@ kOOTextureMagFilterMask
@ kOOTextureExtractChannelMask
@ kOOTextureMinFilterMask
@ kOOTextureMinFilterMipMap
@ kOOTextureMagFilterNearest
@ kOOTextureExtractChannelG
@ kOOTextureExtractChannelR
@ kOOTextureMinFilterNearest
@ kOOTextureDataGrayscaleAlpha
@ kOOTextureDataGrayscale
OOPixMapFormat OOTextureDataFormat
OOTextureInfo gOOTextureInfo
OOTextureDataFormat _format
NSSize originalDimensions()
void ensureFinishedLoading()
NSString * descriptionComponents()
NSString * shortDescriptionComponents()
struct OOPixMap copyPixMapRepresentation()
BOOL isRectangleTexture()
OOTextureLoader * _loader
void uploadTextureDataWithMipMap:format:(BOOL mipMap,[format] OOTextureDataFormat format)
id loaderWithPath:options:(NSString *path,[options] uint32_t options)