Oolite
Loading...
Searching...
No Matches
OOConcreteTexture.m
Go to the documentation of this file.
1/*
2
3 OOConcreteTexture.m
4
5 Copyright (C) 2007-2013 Jens Ayton and contributors
6
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software and associated documentation files (the "Software"), to deal
9 in the Software without restriction, including without limitation the rights
10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 copies of the Software, and to permit persons to whom the Software is
12 furnished to do so, subject to the following conditions:
13
14 The above copyright notice and this permission notice shall be included in all
15 copies or substantial portions of the Software.
16
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 SOFTWARE.
24
25*/
26
27#import "OOConcreteTexture.h"
28#import "OOCPUInfo.h"
30#import "OOMacroOpenGL.h"
32#import "OOPixMap.h"
33#import "OOTextureInternal.h"
34#import "OOTextureLoader.h"
35#import "ResourceManager.h"
36#import "Universe.h"
37
38#ifdef OO_DEBUG_MONITOR_ENABLED
40#endif
41
42#if OOLITE_BIG_ENDIAN
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
46#else
47#error Neither OOLITE_BIG_ENDIAN nor OOLITE_LITTLE_ENDIAN is defined as nonzero!
48#endif
49
50@interface OOConcreteTexture (Private)
51
52- (void)setUpTexture;
53- (void)uploadTexture;
54- (void)uploadTextureDataWithMipMap:(BOOL)mipMap format:(OOTextureDataFormat)format;
55#if OO_TEXTURE_CUBE_MAP
56- (void)uploadTextureCubeMapDataWithMipMap:(BOOL)mipMap format:(OOTextureDataFormat)format;
57#endif
58
59- (GLenum)glTextureTarget;
60
61#if OOTEXTURE_RELOADABLE
62- (BOOL)isReloadable;
63#endif
64
65@end
66
67static BOOL DecodeFormat(OOTextureDataFormat format, uint32_t options, GLenum* outFormat, GLenum* outInternalFormat, GLenum* outType);
68
69@implementation OOConcreteTexture
70
71- (id)initWithLoader:(OOTextureLoader*)loader
72 key:(NSString*)key
73 options:(uint32_t)options
74 anisotropy:(GLfloat)anisotropy
75 lodBias:(GLfloat)lodBias
76{
77 if (loader == nil) {
78 [self release];
79 return nil;
80 }
81
82 self = [super init];
83 if (EXPECT_NOT(self == nil))
84 return nil;
85
86 _loader = [loader retain];
87 _options = options;
88
89#if GL_EXT_texture_filter_anisotropic
90 _anisotropy = OOClamp_0_1_f(anisotropy) * gOOTextureInfo.anisotropyScale;
91#endif
92#if GL_EXT_texture_lod_bias
93 _lodBias = lodBias;
94#endif
95
96#ifdef OO_DEBUG_MONITOR_ENABLED
97 if ([loader isKindOfClass:[OOTextureGenerator class]]) {
98 _name = [[NSString alloc] initWithFormat:@"<%@>", [loader class]];
99 }
100#endif
101
102 _key = [key copy];
103
104 [self addToCaches];
105
106 return self;
107}
108
109- (id)initWithPath:(NSString*)path
110 key:(NSString*)key
111 options:(uint32_t)options
112 anisotropy:(float)anisotropy
113 lodBias:(GLfloat)lodBias
114{
115 OOTextureLoader* loader = [OOTextureLoader loaderWithPath:path options:options];
116 if (loader == nil) {
117 [self release];
118 return nil;
119 }
120
121 if ((self = [self initWithLoader:loader key:key options:options anisotropy:anisotropy lodBias:lodBias])) {
122#if OOTEXTURE_RELOADABLE
123 _path = [path retain];
124#endif
125 }
126
127 return self;
128}
129
130- (void)dealloc
131{
132#ifdef OO_DEBUG_MONITOR_ENABLED
133 OOLog(_trace ? @"texture.allocTrace.dealloc" : @"texture.dealloc", @"Deallocating and uncaching texture %p", self);
134#endif
135
136#if OOTEXTURE_RELOADABLE
137 DESTROY(_path);
138#endif
139
140 if (_loaded) {
141 if (_textureName != 0) {
143 OOGL(glDeleteTextures(1, &_textureName));
144 _textureName = 0;
145 }
146 free(_bytes);
147 _bytes = NULL;
148 }
149
150#ifndef OOTEXTURE_NO_CACHE
151 [self removeFromCaches];
152 [_key autorelease];
153 _key = nil;
154#endif
155
157
158#ifdef OO_DEBUG_MONITOR_ENABLED
159 DESTROY(_name);
160#endif
161
162 [super dealloc];
163}
164
166{
167 NSString* stateDesc = nil;
168
169 if (_loaded) {
170 if (_valid) {
171 stateDesc = [NSString stringWithFormat:@"%u x %u", _width, _height];
172 } else {
173 stateDesc = @"LOAD ERROR";
174 }
175 } else {
176 stateDesc = @"loading";
177 }
178
179 return [NSString stringWithFormat:@"%@, %@", _key, stateDesc];
180}
181
183{
184 return _key;
185}
186
187#ifdef OO_DEBUG_MONITOR_ENABLED
188- (NSString*)name
189{
190 if (_name != nil)
191 return _name;
192
193#if OOTEXTURE_RELOADABLE
194 NSString* name = [_path lastPathComponent];
195#else
196 NSString* name = [[[[self cacheKey] componentsSeparatedByString:@":"] objectAtIndex:0] lastPathComponent];
197#endif
198
199 NSString* channelSuffix = nil;
200 switch (_options & kOOTextureExtractChannelMask) {
202 channelSuffix = @":r";
203 break;
204
206 channelSuffix = @":g";
207 break;
208
210 channelSuffix = @":b";
211 break;
212
214 channelSuffix = @":a";
215 break;
216 }
217
218 if (channelSuffix != nil)
219 name = [name stringByAppendingString:channelSuffix];
220
221 return name;
222}
223#endif
224
225- (void)apply
226{
228
229 if (EXPECT_NOT(!_loaded))
230 [self setUpTexture];
231 else if (EXPECT_NOT(!_uploaded))
232 [self uploadTexture];
233 else
234 OOGL(glBindTexture([self glTextureTarget], _textureName));
235
236#if GL_EXT_texture_lod_bias
237 if (gOOTextureInfo.textureLODBiasAvailable)
238 OOGL(glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT, GL_TEXTURE_LOD_BIAS_EXT, _lodBias));
239#endif
240}
241
243{
244 if (!_loaded)
245 [self setUpTexture];
246}
247
249{
250 return _loaded || [_loader isReady];
251}
252
253- (NSString*)cacheKey
254{
255 return _key;
256}
257
258- (NSSize)dimensions
259{
261
262 return NSMakeSize(_width, _height);
263}
264
266{
268
269 return NSMakeSize(_originalWidth, _originalHeight);
270}
271
273{
275
276 return _mipLevels != 0;
277}
278
280{
282
284
285 if (_bytes != NULL) {
286 // If possible, just copy our existing buffer.
288 px = OODuplicatePixMap(px, 0);
289 }
290#if OOTEXTURE_RELOADABLE
291 else {
292 // Otherwise, read it back from OpenGL.
294
295 GLenum format, internalFormat, type;
296 if (!DecodeFormat(_format, _options, &format, &internalFormat, &type)) {
297 return kOONullPixMap;
298 }
299
300 if (![self isCubeMap]) {
301
303 if (!OOIsValidPixMap(px))
304 return kOONullPixMap;
305
306 glGetTexImage(GL_TEXTURE_2D, 0, format, type, px.pixels);
307 }
308#if OO_TEXTURE_CUBE_MAP
309 else {
310 px = OOAllocatePixMap(_width, _width * 6, _format, 0, 0);
311 if (!OOIsValidPixMap(px))
312 return kOONullPixMap;
313 uint8_t* pixels = px.pixels;
314
315 unsigned i;
316 for (i = 0; i < 6; i++) {
317 glGetTexImage(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, format, type, pixels);
319 }
320 }
321#endif
322 }
323#endif
324
325 return px;
326}
327
329{
330#if GL_EXT_texture_rectangle
331 return _isRectTexture;
332#else
333 return NO;
334#endif
335}
336
338{
339#if OO_TEXTURE_CUBE_MAP
340 return _isCubeMap;
341#else
342 return NO;
343#endif
344}
345
347{
348#if GL_EXT_texture_rectangle
349 if (_loaded) {
350 if (!_isRectTexture) {
351 return NSMakeSize(1.0f, 1.0f);
352 } else {
353 return NSMakeSize(_width, _height);
354 }
355 } else {
356 // Not loaded
358 return NSMakeSize(1.0f, 1.0f);
359 } else {
360 // Finishing may clear the rectangle texture flag (if the texture turns out to be POT)
362 return [self texCoordsScale];
363 }
364 }
365#else
366 return NSMakeSize(1.0f, 1.0f);
367#endif
368}
369
371{
373
374 return _textureName;
375}
376
377@end
378
379@implementation OOConcreteTexture (Private)
380
382{
383 OOPixMap pm;
384
385 // This will block until loading is completed, if necessary.
386 if ([_loader getResult:&pm format:&_format originalWidth:&_originalWidth originalHeight:&_originalHeight]) {
387 _bytes = pm.pixels;
388 _width = pm.width;
389 _height = pm.height;
390
391#if OO_TEXTURE_CUBE_MAP
392 if (_options & kOOTextureAllowCubeMap && _height == _width * 6 && gOOTextureInfo.cubeMapAvailable) {
393 _isCubeMap = YES;
394 }
395#endif
396
397#if defined(OO_DEBUG_MONITOR_ENABLED) && OOTEXTURE_RELOADABLE
398 if (_trace) {
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);
402 OODumpPixMap(pm, name);
403 }
404#endif
405
406 [self uploadTexture];
407 } else {
408 _textureName = 0;
409 _valid = NO;
410 _uploaded = YES;
411 }
412
413 _loaded = YES;
414
415 DESTROY(_loader);
416}
417
419{
420 GLint filter;
421 BOOL mipMap = NO;
422
424
425 if (!_uploaded) {
426 GLenum texTarget = [self glTextureTarget];
427
428 OOGL(glGenTextures(1, &_textureName));
429 OOGL(glBindTexture(texTarget, _textureName));
430
431 // Select wrap mode
432 GLint clampMode = gOOTextureInfo.clampToEdgeAvailable ? GL_CLAMP_TO_EDGE : GL_CLAMP;
433 GLint wrapS = (_options & kOOTextureRepeatS) ? GL_REPEAT : clampMode;
434 GLint wrapT = (_options & kOOTextureRepeatT) ? GL_REPEAT : clampMode;
435
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));
440 }
441#endif
442
443 OOGL(glTexParameteri(texTarget, GL_TEXTURE_WRAP_S, wrapS));
444 OOGL(glTexParameteri(texTarget, GL_TEXTURE_WRAP_T, wrapT));
445
446 // Select min filter
447 filter = _options & kOOTextureMinFilterMask;
448 if (filter == kOOTextureMinFilterNearest)
449 filter = GL_NEAREST;
450 else if (filter == kOOTextureMinFilterMipMap) {
451 mipMap = YES;
452 filter = GL_LINEAR_MIPMAP_LINEAR;
453 } else
454 filter = GL_LINEAR;
455 OOGL(glTexParameteri(texTarget, GL_TEXTURE_MIN_FILTER, filter));
456
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));
460 }
461#endif
462
463 // Select mag filter
464 filter = _options & kOOTextureMagFilterMask;
465 if (filter == kOOTextureMagFilterNearest)
466 filter = GL_NEAREST;
467 else
468 filter = GL_LINEAR;
469 OOGL(glTexParameteri(texTarget, GL_TEXTURE_MAG_FILTER, filter));
470
471 // if (gOOTextureInfo.clientStorageAvailable) EnableClientStorage();
472
473 if (texTarget == GL_TEXTURE_2D) {
474 [self uploadTextureDataWithMipMap:mipMap format:_format];
475 OOLog(@"texture.upload", @"Uploaded texture %u (%ux%u pixels, %@)", _textureName, _width, _height, _key);
476 }
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);
481 }
482#endif
483 else {
484 [NSException raise:NSInternalInconsistencyException format:@"Unhandled texture target 0x%X.", texTarget];
485 }
486
487 _valid = YES;
488 _uploaded = YES;
489
490#if OOTEXTURE_RELOADABLE
491 if ([self isReloadable]) {
492 free(_bytes);
493 _bytes = NULL;
494 }
495#endif
496 }
497}
498
499- (void)uploadTextureDataWithMipMap:(BOOL)mipMap format:(OOTextureDataFormat)format
500{
501 GLenum glFormat = 0, internalFormat = 0, type = 0;
502 unsigned w = _width,
503 h = _height,
504 level = 0;
505 char* bytes = _bytes;
506 uint8_t components = OOTextureComponentsForFormat(format);
507
509
510 if (!DecodeFormat(format, _options, &glFormat, &internalFormat, &type))
511 return;
512
513 while (0 < w && 0 < h) {
514 OOGL(glTexImage2D(GL_TEXTURE_2D, level++, internalFormat, w, h, 0, glFormat, type, bytes));
515 if (!mipMap)
516 return;
517 bytes += w * components * h;
518 w >>= 1;
519 h >>= 1;
520 }
521
522 // Note: we only reach here if (mipMap).
523 _mipLevels = level - 1;
524 OOGL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, _mipLevels));
525}
526
527#if OO_TEXTURE_CUBE_MAP
528- (void)uploadTextureCubeMapDataWithMipMap:(BOOL)mipMap format:(OOTextureDataFormat)format
529{
531
532 GLenum glFormat = 0, internalFormat = 0, type = 0;
533 if (!DecodeFormat(format, _options, &glFormat, &internalFormat, &type))
534 return;
535 uint8_t components = OOTextureComponentsForFormat(format);
536
537 // Calculate stride between cube map sides.
538 size_t sideSize = _width * _width * components;
539 if (mipMap) {
540 sideSize = sideSize * 4 / 3;
541 sideSize = (sideSize + 15) & ~15;
542 }
543
544 unsigned side;
545 for (side = 0; side < 6; side++) {
546 char* bytes = _bytes;
547 bytes += side * sideSize;
548
549 unsigned w = _width, level = 0;
550
551 while (0 < w) {
552 OOGL(glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + side, level++, internalFormat, w, w, 0, glFormat, type, bytes));
553 if (!mipMap)
554 break;
555 bytes += w * w * components;
556 w >>= 1;
557 }
558 }
559}
560#endif
561
563{
564 GLenum texTarget = GL_TEXTURE_2D;
565#if OO_TEXTURE_CUBE_MAP
566 if (_isCubeMap) {
567 texTarget = GL_TEXTURE_CUBE_MAP;
568 }
569#endif
570 return texTarget;
571}
572
574{
575 if (_loaded && _uploaded && _valid) {
577
578 _uploaded = NO;
579 OOGL(glDeleteTextures(1, &_textureName));
580 _textureName = 0;
581
582#if OOTEXTURE_RELOADABLE
583 if ([self isReloadable]) {
584 OOLog(@"texture.reload", @"Reloading texture %@", self);
585
586 free(_bytes);
587 _bytes = NULL;
588 _loaded = NO;
589 _uploaded = NO;
590 _valid = NO;
591
592 _loader = [[OOTextureLoader loaderWithPath:_path options:_options] retain];
593 }
594#endif
595 }
596}
597
598#if OOTEXTURE_RELOADABLE
599
601{
602 return _path != nil;
603}
604
605#endif
606
607@end
608
609static BOOL DecodeFormat(OOTextureDataFormat format, uint32_t options, GLenum* outFormat, GLenum* outInternalFormat, GLenum* outType)
610{
611 NSCParameterAssert(outFormat != NULL && outInternalFormat != NULL && outType != NULL);
612
613 switch (format) {
615 *outFormat = GL_RGBA;
616 *outInternalFormat = options & kOOTextureSRGBA ? GL_SRGB_ALPHA : GL_RGBA;
617 *outType = RGBA_IMAGE_TYPE;
618 return YES;
619
621 if (options & kOOTextureAlphaMask) {
622 *outFormat = GL_ALPHA;
623 *outInternalFormat = GL_ALPHA8;
624 } else {
625 *outFormat = GL_LUMINANCE;
626 *outInternalFormat = GL_LUMINANCE8;
627 }
628 *outType = GL_UNSIGNED_BYTE;
629 return YES;
630
632 *outFormat = GL_LUMINANCE_ALPHA;
633 *outInternalFormat = GL_LUMINANCE8_ALPHA8;
634 *outType = GL_UNSIGNED_BYTE;
635 return YES;
636
637 default:
638 OOLog(kOOLogParameterError, @"Unexpected texture format %u.", format);
639 return NO;
640 }
641}
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 DESTROY(x)
#define EXPECT_NOT(x)
#define OOLog(class, format,...)
Definition OOLogging.h:84
NSString *const kOOLogParameterError
Definition OOLogging.m:596
#define OO_ENTER_OPENGL()
#define OOGL(statement)
Definition OOOpenGL.h:262
void OODumpPixMap(OOPixMap pixMap, NSString *name)
Definition OOPixMap.m:140
unsigned short OOPixMapBytesPerPixelForFormat(OOPixMapFormat format) PURE_FUNC
Definition OOPixMap.m:195
const OOPixMap kOONullPixMap
Definition OOPixMap.m:30
OOPixMap OOAllocatePixMap(OOPixMapDimension width, OOPixMapDimension height, OOPixMapFormat format, size_t rowBytes, size_t bufferSize)
Definition OOPixMap.m:66
OOPixMap OOMakePixMap(void *pixels, OOPixMapDimension width, OOPixMapDimension height, OOPixMapFormat format, size_t rowBytes, size_t bufferSize)
Definition OOPixMap.m:44
BOOL OOIsValidPixMap(OOPixMap pixMap)
Definition OOPixMap.m:39
OOPixMap OODuplicatePixMap(OOPixMap srcPixMap, size_t desiredSize)
Definition OOPixMap.m:89
return nil
#define GL_TEXTURE_MAX_LEVEL
#define GL_CLAMP_TO_EDGE
uint8_t OOTextureComponentsForFormat(OOTextureDataFormat format)
Definition OOTexture.m:624
@ kOOTextureRepeatS
Definition OOTexture.h:55
@ kOOTextureAllowRectTexture
Definition OOTexture.h:57
@ kOOTextureExtractChannelA
Definition OOTexture.h:70
@ kOOTextureExtractChannelB
Definition OOTexture.h:69
@ kOOTextureMagFilterMask
Definition OOTexture.h:73
@ kOOTextureExtractChannelMask
Definition OOTexture.h:65
@ kOOTextureAllowCubeMap
Definition OOTexture.h:61
@ kOOTextureMinFilterMask
Definition OOTexture.h:72
@ kOOTextureSRGBA
Definition OOTexture.h:63
@ kOOTextureRepeatT
Definition OOTexture.h:56
@ kOOTextureMinFilterMipMap
Definition OOTexture.h:48
@ kOOTextureAlphaMask
Definition OOTexture.h:60
@ kOOTextureMagFilterNearest
Definition OOTexture.h:50
@ kOOTextureExtractChannelG
Definition OOTexture.h:68
@ kOOTextureExtractChannelR
Definition OOTexture.h:67
@ kOOTextureMinFilterNearest
Definition OOTexture.h:46
@ kOOTextureDataGrayscaleAlpha
Definition OOTexture.h:105
@ kOOTextureDataGrayscale
Definition OOTexture.h:104
@ kOOTextureDataRGBA
Definition OOTexture.h:103
OOPixMapFormat OOTextureDataFormat
Definition OOTexture.h:107
OOTextureInfo gOOTextureInfo
Definition OOTexture.m:97
OOTextureDataFormat _format
NSString * descriptionComponents()
NSString * shortDescriptionComponents()
struct OOPixMap copyPixMapRepresentation()
OOTextureLoader * _loader
void uploadTextureDataWithMipMap:format:(BOOL mipMap,[format] OOTextureDataFormat format)
id loaderWithPath:options:(NSString *path,[options] uint32_t options)
void addToCaches()
Definition OOTexture.m:412
void dealloc()
Definition OOTexture.m:251
void removeFromCaches()
Definition OOTexture.m:441
OOPixMapDimension height
Definition OOPixMap.h:45
void * pixels
Definition OOPixMap.h:44
OOPixMapDimension width
Definition OOPixMap.h:45