Oolite
Loading...
Searching...
No Matches
OOTexture.m
Go to the documentation of this file.
1/*
2
3 OOTexture.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 "OOTexture.h"
28#import "OOConcreteTexture.h"
29#import "OONullTexture.h"
30#import "OOTextureInternal.h"
31
33#import "OOTextureLoader.h"
34
35#import "OOCPUInfo.h"
36#import "OOCache.h"
38#import "OOMacroOpenGL.h"
40#import "OOPixMap.h"
41#import "ResourceManager.h"
42#import "Universe.h"
43
44NSString* const kOOTextureSpecifierNameKey = @"name";
45NSString* const kOOTextureSpecifierSwizzleKey = @"extract_channel";
46NSString* const kOOTextureSpecifierMinFilterKey = @"min_filter";
47NSString* const kOOTextureSpecifierMagFilterKey = @"mag_filter";
48NSString* const kOOTextureSpecifierNoShrinkKey = @"no_shrink";
49NSString* const kOOTextureSpecifierExtraShrinkKey = @"extra_shrink";
50NSString* const kOOTextureSpecifierRepeatSKey = @"repeat_s";
51NSString* const kOOTextureSpecifierRepeatTKey = @"repeat_t";
52NSString* const kOOTextureSpecifierCubeMapKey = @"cube_map";
53NSString* const kOOTextureSpecifierAnisotropyKey = @"anisotropy";
54NSString* const kOOTextureSpecifierLODBiasKey = @"texture_LOD_bias";
55
56NSString* const kOOTextureSpecifierModulateColorKey = @"color";
57NSString* const kOOTextureSpecifierIlluminationModeKey = @"illumination_mode";
58NSString* const kOOTextureSpecifierSelfColorKey = @"self_color";
59NSString* const kOOTextureSpecifierScaleFactorKey = @"scale_factor";
60NSString* const kOOTextureSpecifierBindingKey = @"binding";
61
62// Used only by "internal" specifiers from OOMakeTextureSpecifier.
63static NSString* const kOOTextureSpecifierFlagValueInternalKey = @"_oo_internal_flags";
64
65/* Texture caching:
66 two and a half parallel caching mechanisms are used. sLiveTextureCache
67 tracks all live texture objects with cache keys, without retaining them
68 (using NSValues to refer to the objects).
69
70 sAllLiveTextures tracks all textures, including ones without cache keys,
71 so that they can be notified of graphics resets. This also uses NSValues
72 to avoid retaining the textures.
73
74 sRecentTextures tracks up to kRecentTexturesCount textures which
75 have been used recently, and retains them.
76
77 This means that the number of live texture objects will never fall below
78 80% of kRecentTexturesCount (80% comes from the behaviour of OOCache), but
79 old textures will eventually be released. If the number of active textures
80 exceeds kRecentTexturesCount, all of them will be reusable through
81 sLiveTextureCache, but only a most-recently-fetched subset will be kept
82 around by the cache when the number drops.
83
84 Note the textures in sRecentTextures are a superset of the textures in
85 sLiveTextureCache, and the textures in sLiveTextureCache are a superset
86 of sRecentTextures.
87*/
88enum {
90};
91
92static NSMutableDictionary* sLiveTextureCache;
93static NSMutableSet* sAllLiveTextures;
95
98
99@interface OOTexture (OOPrivate)
100
101- (void)addToCaches;
102+ (OOTexture*)existingTextureForKey:(NSString*)key;
103
104- (void)forceRebind;
105
107
108#ifdef OO_DEBUG_MONITOR_ENABLED
109- (id)retainInContext:(NSString*)context;
110- (void)releaseInContext:(NSString*)context;
111- (id)autoreleaseInContext:(NSString*)context;
112#endif
113
114@end
115
116#ifdef OO_DEBUG_MONITOR_ENABLED
117static NSString* sGlobalTraceContext = nil;
118
119#define SET_TRACE_CONTEXT(str) \
120 do { \
121 sGlobalTraceContext = (str); \
122 } while (0)
123#else
124#define SET_TRACE_CONTEXT(str) \
125 do { \
126 } while (0)
127#endif
128#define CLEAR_TRACE_CONTEXT() SET_TRACE_CONTEXT(nil)
129
130@implementation OOTexture
131
132+ (id)textureWithName:(NSString*)name
133 inFolder:(NSString*)directory
134 options:(OOTextureFlags)options
135 anisotropy:(GLfloat)anisotropy
136 lodBias:(GLfloat)lodBias
137{
138 NSString* key = nil;
139 OOTexture* result = nil;
140 NSString* path = nil;
141 BOOL noFNF;
142
143 if (EXPECT_NOT(name == nil))
144 return nil;
146 [self checkExtensions];
147
148 if (!gOOTextureInfo.anisotropyAvailable || (options & kOOTextureMinFilterMask) != kOOTextureMinFilterMipMap) {
149 anisotropy = 0.0f;
150 }
151 if (!gOOTextureInfo.textureLODBiasAvailable || (options & kOOTextureMinFilterMask) != kOOTextureMinFilterMipMap) {
152 lodBias = 0.0f;
153 }
154
155 noFNF = (options & kOOTextureNoFNFMessage) != 0;
157
158 // Look for existing texture
159 key = OOGenerateTextureCacheKey(directory, name, options, anisotropy, lodBias);
160 result = [OOTexture existingTextureForKey:key];
161 if (result == nil) {
162 path = [ResourceManager pathForFileNamed:name inFolder:directory];
163 if (path == nil) {
164 if (!noFNF)
165 OOLogWARN(kOOLogFileNotFound, @"Could not find texture file \"%@\".", name);
166 return nil;
167 }
168
169 // No existing texture, load texture.
170 result = [[[OOConcreteTexture alloc] initWithPath:path key:key options:options anisotropy:anisotropy lodBias:lodBias] autorelease];
171 }
172
173 return result;
174}
175
176+ (id)textureWithName:(NSString*)name
177 inFolder:(NSString*)directory
178{
179 return [self textureWithName:name
180 inFolder:directory
181 options:kOOTextureDefaultOptions
182 anisotropy:kOOTextureDefaultAnisotropy
183 lodBias:kOOTextureDefaultLODBias];
184}
185
186+ (id)textureWithConfiguration:(id)configuration
187{
188 return [self textureWithConfiguration:configuration extraOptions:0];
189}
190
191+ (id)textureWithConfiguration:(id)configuration extraOptions:(OOTextureFlags)extraOptions
192{
193 NSString* name = nil;
194 OOTextureFlags options = 0;
195 GLfloat anisotropy = 0.0f;
196 GLfloat lodBias = 0.0f;
197
198 if (!OOInterpretTextureSpecifier(configuration, &name, &options, &anisotropy, &lodBias, NO))
199 return nil;
200
201 return [self textureWithName:name inFolder:@"Textures" options:options | extraOptions anisotropy:anisotropy lodBias:lodBias];
202}
203
205{
207}
209+ (id)textureWithGenerator:(OOTextureGenerator*)generator
210{
211 return [self textureWithGenerator:generator enqueue:NO];
214+ (id)textureWithGenerator:(OOTextureGenerator*)generator enqueue:(BOOL)enqueue
215{
216 if (generator == nil)
217 return nil;
218
219#ifndef OOTEXTURE_NO_CACHE
221 if (existing != nil && !enqueue)
222 return [[existing retain] autorelease];
223#endif
224
225 if (![generator enqueue]) {
226 OOLogERR(@"texture.generator.queue.failed", @"Failed to queue generator %@", generator);
227 return nil;
228 }
229 OOLog(@"texture.generator.queue", @"Queued texture generator %@", generator);
230
231 OOTexture* result = [[[OOConcreteTexture alloc] initWithLoader:generator
232 key:[generator cacheKey]
233 options:OOApplyTextureOptionDefaults([generator textureOptions])
234 anisotropy:[generator anisotropy]
235 lodBias:[generator lodBias]] autorelease];
236
237 return result;
238}
240- (id)init
241{
242 if ((self = [super init])) {
244 sAllLiveTextures = [[NSMutableSet alloc] init];
245 [sAllLiveTextures addObject:[NSValue valueWithPointer:self]];
246 }
247
248 return self;
249}
250
251- (void)dealloc
252{
253 [sAllLiveTextures removeObject:[NSValue valueWithPointer:self]];
254
255 [super dealloc];
256}
257
262
264{
266 OOGL(glBindTexture(GL_TEXTURE_2D, 0));
267#if OO_TEXTURE_CUBE_MAP
269 OOGL(glBindTexture(GL_TEXTURE_CUBE_MAP, 0));
270#endif
271
272#if GL_EXT_texture_lod_bias
273 if (gOOTextureInfo.textureLODBiasAvailable)
274 OOGL(glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT, GL_TEXTURE_LOD_BIAS_EXT, 0));
275#endif
276}
277
279{
280}
281
283{
284 return YES;
285}
286
287- (NSString*)cacheKey
288{
289 return nil;
290}
291
292- (NSSize)dimensions
293{
295 return NSZeroSize;
296}
297
299{
300 return [self dimensions];
301}
302
304{
306 return NO;
307}
308
310{
311 return kOONullPixMap;
312}
313
315{
316 return NO;
317}
318
320{
321 return NO;
322}
323
325{
326 return NSMakeSize(1.0, 1.0);
327}
328
330{
332 return 0;
333}
334
336{
337 /* Does not clear sAllLiveTextures - that really must refer to all
338 live texture objects.
339 */
340 SET_TRACE_CONTEXT(@"clearing sLiveTextureCache");
341 [sLiveTextureCache autorelease];
343
344 SET_TRACE_CONTEXT(@"clearing sRecentTextures");
345 [sRecentTextures autorelease];
348}
349
351{
352 NSEnumerator* textureEnum = nil;
353 id texture = nil;
354
355 // Keeping around unused, cached textures is unhelpful at this point.
357
358 for (textureEnum = [sAllLiveTextures objectEnumerator]; (texture = [[textureEnum nextObject] pointerValue]);) {
359 [texture forceRebind];
360 }
361}
362
363#ifdef OO_DEBUG_MONITOR_ENABLED
364- (void)setTrace:(BOOL)trace
365{
366 if (trace && !_trace) {
367 OOLog(@"texture.allocTrace.begin", @"Started tracing texture %p with retain count %zu.", self, [self retainCount]);
368 }
369 _trace = trace;
370}
371
372+ (NSArray*)cachedTexturesByAge
373{
375}
376
377+ (NSSet*)allTextures
378{
379 NSMutableSet* result = [NSMutableSet setWithCapacity:[sAllLiveTextures count]];
380 NSValue* box = nil;
381 foreach (box, sAllLiveTextures) {
382 [result addObject:[box pointerValue]];
383 }
384
385 return result;
386}
387
388- (size_t)dataSize
389{
390 NSSize dimensions = [self dimensions];
391 size_t size = dimensions.width * dimensions.height;
392 if ([self isCubeMap])
393 size *= 6;
394 if ([self isMipMapped])
395 size = size * 4 / 3;
396
397 return size;
398}
399
400- (NSString*)name
401{
403 return nil;
404}
405#endif
406
411
413{
414#ifndef OOTEXTURE_NO_CACHE
415 NSString* cacheKey = [self cacheKey];
416 if (cacheKey == nil)
417 return;
418
419 // Add self to in-use textures cache, wrapped in an NSValue so the texture isn't retained by the cache.
421 sLiveTextureCache = [[NSMutableDictionary alloc] init];
422
423 SET_TRACE_CONTEXT(@"in-use textures cache - SHOULD NOT RETAIN");
424 [sLiveTextureCache setObject:[NSValue valueWithPointer:self] forKey:cacheKey];
426
427 // Add self to recent textures cache.
429 sRecentTextures = [[OOCache alloc] init];
430 [sRecentTextures setName:@"recent textures"];
432 [sRecentTextures setPruneThreshold:kRecentTexturesCount];
433 }
434
435 SET_TRACE_CONTEXT(@"adding to recent textures cache");
436 [sRecentTextures setObject:self forKey:cacheKey];
438#endif
439}
440
442{
443#ifndef OOTEXTURE_NO_CACHE
444 NSString* cacheKey = [self cacheKey];
445 if (cacheKey == nil)
446 return;
447
448 [sLiveTextureCache removeObjectForKey:cacheKey];
449 if (EXPECT_NOT([sRecentTextures objectForKey:cacheKey] == self)) {
450 /* Experimental for now: I think the recent crash problems may
451 * be because if the last reference to a texture is in
452 * sRecentTextures, and the texture is regenerated, it
453 * replaces the texture, causing a release. Therefore, if this
454 * texture *isn't* overretained in the texture cache, the 2009
455 * crash avoider will delete its replacement from the cache
456 * ... possibly before that texture has been fully added to
457 * the cache itself. So, the texture is only removed from the
458 * cache by key if it was in it with that key. The extra time
459 * needed to generate a planet texture compared with loading a
460 * standard one may be why this problem shows up. - CIM 20140122
461 */
462 NSAssert2(0, @"Texture retain count error for %@; cacheKey is %@.", self, cacheKey); // miscount in autorelease
463 // The following line is needed in order to avoid crashes when there's a 'texture retain count error'. Please do not delete. -- Kaks 20091221
464 [sRecentTextures removeObjectForKey:cacheKey]; // make sure there's no reference left inside sRecentTexture ( was a show stopper for 1.73)
465 }
466#endif
467}
468
469+ (OOTexture*)existingTextureForKey:(NSString*)key
470{
471#ifndef OOTEXTURE_NO_CACHE
472 if (key != nil) {
473 return (OOTexture*)[[sLiveTextureCache objectForKey:key] pointerValue];
474 }
475 return nil;
476#else
477 return nil;
478#endif
479}
480
482{
484
485 sCheckedExtensions = YES;
486
488 BOOL ver120 = [extMgr versionIsAtLeastMajor:1 minor:2];
489 BOOL ver130 = [extMgr versionIsAtLeastMajor:1 minor:3];
490
491#if GL_EXT_texture_filter_anisotropic
492 gOOTextureInfo.anisotropyAvailable = [extMgr haveExtension:@"GL_EXT_texture_filter_anisotropic"];
493 OOGL(glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &gOOTextureInfo.anisotropyScale));
494 gOOTextureInfo.anisotropyScale *= OOClamp_0_1_f([[NSUserDefaults standardUserDefaults] oo_floatForKey:@"texture-anisotropy-scale" defaultValue:0.5]);
495#endif
496
497#ifdef GL_CLAMP_TO_EDGE
498 gOOTextureInfo.clampToEdgeAvailable = ver120 || [extMgr haveExtension:@"GL_SGIS_texture_edge_clamp"];
499#endif
500
501#if OO_GL_CLIENT_STORAGE
502 gOOTextureInfo.clientStorageAvailable = [extMgr haveExtension:@"GL_APPLE_client_storage"];
503#endif
504
505 gOOTextureInfo.textureMaxLevelAvailable = ver120 || [extMgr haveExtension:@"GL_SGIS_texture_lod"];
506
507#if GL_EXT_texture_lod_bias
508 if ([[NSUserDefaults standardUserDefaults] oo_boolForKey:@"use-texture-lod-bias" defaultValue:YES]) {
509 gOOTextureInfo.textureLODBiasAvailable = [extMgr haveExtension:@"GL_EXT_texture_lod_bias"];
510 } else {
511 gOOTextureInfo.textureLODBiasAvailable = NO;
512 }
513#endif
514
515#if GL_EXT_texture_rectangle
516 gOOTextureInfo.rectangleTextureAvailable = [extMgr haveExtension:@"GL_EXT_texture_rectangle"];
517#endif
518
519#if OO_TEXTURE_CUBE_MAP
520 if (![[NSUserDefaults standardUserDefaults] boolForKey:@"disable-cube-maps"]) {
521 gOOTextureInfo.cubeMapAvailable = ver130 || [extMgr haveExtension:@"GL_ARB_texture_cube_map"];
522 } else {
523 gOOTextureInfo.cubeMapAvailable = NO;
524 }
525
526#endif
527}
528
529#ifdef OO_DEBUG_MONITOR_ENABLED
530- (id)retainInContext:(NSString*)context
531{
532 if (_trace) {
533 if (context)
534 OOLog(@"texture.allocTrace.retain", @"Texture %p retained (retain count -> %zu) - %@.", self, [self retainCount] + 1, context);
535 else
536 OOLog(@"texture.allocTrace.retain", @"Texture %p retained (retain count -> %zu).", self, [self retainCount] + 1);
537 }
538
539 return [super retain];
540}
541
542- (void)releaseInContext:(NSString*)context
543{
544 if (_trace) {
545 if (context)
546 OOLog(@"texture.allocTrace.release", @"Texture %p released (retain count -> %zu) - %@.", self, [self retainCount] - 1, context);
547 else
548 OOLog(@"texture.allocTrace.release", @"Texture %p released (retain count -> %zu).", self, [self retainCount] - 1);
549 }
550
551 [super release];
552}
553
554- (id)autoreleaseInContext:(NSString*)context
555{
556 if (_trace) {
557 if (context)
558 OOLog(@"texture.allocTrace.autoreleased", @"Texture %p autoreleased - %@.", self, context);
559 else
560 OOLog(@"texture.allocTrace.autoreleased", @"Texture %p autoreleased.", self);
561 }
562
563 return [super autorelease];
564}
565
566- (id)retain
567{
568 return [self retainInContext:sGlobalTraceContext];
569}
570
571- (oneway void)release
572{
573 [self releaseInContext:sGlobalTraceContext];
574}
575
576- (id)autorelease
577{
578 return [self autoreleaseInContext:sGlobalTraceContext];
579}
580#endif
581
582@end
583
584@implementation NSDictionary (OOTextureConveniences)
585
586- (NSDictionary*)oo_textureSpecifierForKey:(id)key defaultName:(NSString*)name
587{
588 return OOTextureSpecFromObject([self objectForKey:key], name);
589}
590
591@end
592
593@implementation NSArray (OOTextureConveniences)
594
595- (NSDictionary*)oo_textureSpecifierAtIndex:(unsigned)index defaultName:(NSString*)name
596{
597 return OOTextureSpecFromObject([self objectAtIndex:index], name);
598}
599
600@end
601
602NSDictionary* OOTextureSpecFromObject(id object, NSString* defaultName)
603{
604 if (object == nil)
605 object = defaultName;
606 if ([object isKindOfClass:[NSString class]]) {
607 if ([object isEqualToString:@""])
608 return nil;
609 return [NSDictionary dictionaryWithObject:object forKey:@"name"];
610 }
611 if (![object isKindOfClass:[NSDictionary class]])
612 return nil;
613
614 // If we're here, it's a dictionary.
615 if (defaultName == nil || [object oo_stringForKey:@"name"] != nil)
616 return object;
617
618 // If we get here, there's no "name" key and there is a default, so we fill it in:
619 NSMutableDictionary* mutableResult = [NSMutableDictionary dictionaryWithDictionary:object];
620 [mutableResult setObject:[[defaultName copy] autorelease] forKey:@"name"];
621 return mutableResult;
622}
623
625{
626 switch (format) {
628 return 4;
629
631 return 1;
632
634 return 2;
635
637 break;
638 }
639
640 return 0;
641}
642
644{
645 return gOOTextureInfo.cubeMapAvailable;
646}
647
648BOOL OOInterpretTextureSpecifier(id specifier, NSString** outName, OOTextureFlags* outOptions, float* outAnisotropy, float* outLODBias, BOOL ignoreExtract)
649{
650 NSString* name = nil;
652 float anisotropy = kOOTextureDefaultAnisotropy;
653 float lodBias = kOOTextureDefaultLODBias;
654
655 if ([specifier isKindOfClass:[NSString class]]) {
656 name = specifier;
657 } else if ([specifier isKindOfClass:[NSDictionary class]]) {
658 name = [specifier oo_stringForKey:kOOTextureSpecifierNameKey];
659 if (name == nil) {
660 OOLog(@"texture.load.noName", @"Invalid texture configuration dictionary (must specify name):\n%@", specifier);
661 return NO;
662 }
663
664 int quickFlags = [specifier oo_intForKey:kOOTextureSpecifierFlagValueInternalKey defaultValue:-1];
665 if (quickFlags != -1) {
666 options = quickFlags;
667 } else {
668 NSString* filterString = [specifier oo_stringForKey:kOOTextureSpecifierMinFilterKey defaultValue:@"default"];
669 if ([filterString isEqualToString:@"nearest"])
671 else if ([filterString isEqualToString:@"linear"])
672 options |= kOOTextureMinFilterLinear;
673 else if ([filterString isEqualToString:@"mipmap"])
674 options |= kOOTextureMinFilterMipMap;
675 else
676 options |= kOOTextureMinFilterDefault; // Covers "default"
677
678 filterString = [specifier oo_stringForKey:kOOTextureSpecifierMagFilterKey defaultValue:@"default"];
679 if ([filterString isEqualToString:@"nearest"])
681 else
682 options |= kOOTextureMagFilterLinear; // Covers "default" and "linear"
683
684 if ([specifier oo_boolForKey:kOOTextureSpecifierNoShrinkKey defaultValue:NO])
685 options |= kOOTextureNoShrink;
686 if ([specifier oo_boolForKey:kOOTextureSpecifierExtraShrinkKey defaultValue:NO])
687 options |= kOOTextureExtraShrink;
688 if ([specifier oo_boolForKey:kOOTextureSpecifierRepeatSKey defaultValue:NO])
689 options |= kOOTextureRepeatS;
690 if ([specifier oo_boolForKey:kOOTextureSpecifierRepeatTKey defaultValue:NO])
691 options |= kOOTextureRepeatT;
692 if ([specifier oo_boolForKey:kOOTextureSpecifierCubeMapKey defaultValue:NO])
693 options |= kOOTextureAllowCubeMap;
694
695 if (!ignoreExtract) {
696 NSString* extractChannel = [specifier oo_stringForKey:@"extract_channel"];
697 if (extractChannel != nil) {
698 if ([extractChannel isEqualToString:@"r"])
699 options |= kOOTextureExtractChannelR;
700 else if ([extractChannel isEqualToString:@"g"])
701 options |= kOOTextureExtractChannelG;
702 else if ([extractChannel isEqualToString:@"b"])
703 options |= kOOTextureExtractChannelB;
704 else if ([extractChannel isEqualToString:@"a"])
705 options |= kOOTextureExtractChannelA;
706 else {
707 OOLogWARN(@"texture.load.extractChannel.invalid", @"Unknown value \"%@\" for extract_channel in specifier \"%@\" (should be \"r\", \"g\", \"b\" or \"a\").", extractChannel, specifier);
708 }
709 }
710 }
711 }
712 anisotropy = [specifier oo_floatForKey:@"anisotropy" defaultValue:kOOTextureDefaultAnisotropy];
713 lodBias = [specifier oo_floatForKey:@"texture_LOD_bias" defaultValue:kOOTextureDefaultLODBias];
714 } else {
715 // Bad type
716 if (specifier != nil)
717 OOLog(kOOLogParameterError, @"%s: expected string or dictionary, got %@.", __PRETTY_FUNCTION__, [specifier class]);
718 return NO;
719 }
720
721 if ([name length] == 0)
722 return NO;
723
724 if (outName != NULL)
725 *outName = name;
726 if (outOptions != NULL)
727 *outOptions = options;
728 if (outAnisotropy != NULL)
729 *outAnisotropy = anisotropy;
730 if (outLODBias != NULL)
731 *outLODBias = lodBias;
732
733 return YES;
734}
735
736NSDictionary* OOMakeTextureSpecifier(NSString* name, OOTextureFlags options, float anisotropy, float lodBias, BOOL internal)
737{
738 NSMutableDictionary* result = [NSMutableDictionary dictionary];
739
740 [result setObject:name forKey:kOOTextureSpecifierNameKey];
741
742 if (anisotropy != kOOTextureDefaultAnisotropy)
743 [result oo_setFloat:anisotropy forKey:kOOTextureSpecifierAnisotropyKey];
744 if (lodBias != kOOTextureDefaultLODBias)
745 [result oo_setFloat:lodBias forKey:kOOTextureSpecifierLODBiasKey];
746
747 if (internal) {
748 [result oo_setUnsignedInteger:options forKey:kOOTextureSpecifierFlagValueInternalKey];
749 } else {
750 NSString* value = nil;
751 switch (options & kOOTextureMinFilterMask) {
753 break;
754
756 value = @"nearest";
757 break;
758
760 value = @"linear";
761 break;
762
764 value = @"mipmap";
765 break;
766 }
767 if (value != nil)
768 [result setObject:value forKey:kOOTextureSpecifierNoShrinkKey];
769
770 value = nil;
771 switch (options & kOOTextureMagFilterMask) {
773 value = @"nearest";
774 break;
775
777 break;
778 }
779 if (value != nil)
780 [result setObject:value forKey:kOOTextureSpecifierMagFilterKey];
781
782 value = nil;
783 switch (options & kOOTextureExtractChannelMask) {
785 break;
786
788 value = @"r";
789 break;
790
792 value = @"g";
793 break;
794
796 value = @"b";
797 break;
798
800 value = @"a";
801 break;
802 }
803 if (value != nil)
804 [result setObject:value forKey:kOOTextureSpecifierSwizzleKey];
805
806 if (options & kOOTextureNoShrink)
807 [result oo_setBool:YES forKey:kOOTextureSpecifierNoShrinkKey];
808 if (options & kOOTextureRepeatS)
809 [result oo_setBool:YES forKey:kOOTextureSpecifierRepeatSKey];
810 if (options & kOOTextureRepeatT)
811 [result oo_setBool:YES forKey:kOOTextureSpecifierRepeatTKey];
812 if (options & kOOTextureAllowCubeMap)
813 [result oo_setBool:YES forKey:kOOTextureSpecifierCubeMapKey];
814 }
815
816 return result;
817}
818
820{
821 // Set default flags if needed
823 if ([UNIVERSE reducedDetail]) {
824 options |= kOOTextureMinFilterLinear;
825 } else {
826 options |= kOOTextureMinFilterMipMap;
827 }
828 }
829
830 if (!gOOTextureInfo.textureMaxLevelAvailable) {
831 /* In the unlikely case of an OpenGL system without GL_SGIS_texture_lod,
832 disable mip-mapping completely. Strictly this is only needed for
833 non-square textures, but extra logic for such a rare case isn't
834 worth it.
835 */
838 }
839 }
840
841 if (options & kOOTextureAllowRectTexture) {
842 // Apply rectangle texture restrictions (regardless of whether rectangle textures are available, for consistency)
846 }
847
848#if GL_EXT_texture_rectangle
849 if (!gOOTextureInfo.rectangleTextureAvailable) {
851 }
852#else
854#endif
855 }
856
857 options &= kOOTextureDefinedFlags;
858
859 return options;
860}
861
862NSString* OOGenerateTextureCacheKey(NSString* directory, NSString* name, OOTextureFlags options, float anisotropy, float lodBias)
863{
864 if (!gOOTextureInfo.anisotropyAvailable || (options & kOOTextureMinFilterMask) != kOOTextureMinFilterMipMap) {
865 anisotropy = 0.0f;
866 }
867 if (!gOOTextureInfo.textureLODBiasAvailable || (options & kOOTextureMinFilterMask) != kOOTextureMinFilterMipMap) {
868 lodBias = 0.0f;
869 }
871
872 return [NSString stringWithFormat:@"%@%@%@:0x%.4X/%g/%g", directory ? directory : (NSString*)@"", directory ? @"/" : @"", name, options, anisotropy, lodBias];
873}
874
875NSString* OOTextureCacheKeyForSpecifier(id specifier)
876{
877 NSString* name;
878 OOTextureFlags options;
879 float anisotropy;
880 float lodBias;
881
882 OOInterpretTextureSpecifier(specifier, &name, &options, &anisotropy, &lodBias, NO);
883 return OOGenerateTextureCacheKey(@"Textures", name, options, anisotropy, lodBias);
884}
OOINLINE float OOClamp_0_1_f(float value) INLINE_CONST_FUNC
#define DESTROY(x)
#define EXPECT_NOT(x)
#define OOLogWARN(class, format,...)
Definition OOLogging.h:126
#define OOLogERR(class, format,...)
Definition OOLogging.h:125
#define OOLogGenericSubclassResponsibility()
Definition OOLogging.h:141
#define OOLog(class, format,...)
Definition OOLogging.h:84
NSString *const kOOLogParameterError
Definition OOLogging.m:596
NSString *const kOOLogFileNotFound
Definition OOLogging.m:601
#define OO_ENTER_OPENGL()
#define OOGL(statement)
Definition OOOpenGL.h:262
const OOPixMap kOONullPixMap
Definition OOPixMap.m:30
return nil
NSString *const kOOTextureSpecifierSelfColorKey
Definition OOTexture.m:58
NSString *const kOOTextureSpecifierNoShrinkKey
Definition OOTexture.m:48
NSString *const kOOTextureSpecifierNameKey
Definition OOTexture.m:44
BOOL OOCubeMapsAvailable(void)
Definition OOTexture.m:643
uint8_t OOTextureComponentsForFormat(OOTextureDataFormat format)
Definition OOTexture.m:624
NSDictionary * OOMakeTextureSpecifier(NSString *name, OOTextureFlags options, float anisotropy, float lodBias, BOOL internal)
Definition OOTexture.m:736
NSString *const kOOTextureSpecifierRepeatTKey
Definition OOTexture.m:51
OOTextureFlags OOApplyTextureOptionDefaults(OOTextureFlags options)
Definition OOTexture.m:819
NSDictionary * OOTextureSpecFromObject(id object, NSString *defaultName)
Definition OOTexture.m:602
NSString *const kOOTextureSpecifierScaleFactorKey
Definition OOTexture.m:59
@ kOOTextureRepeatS
Definition OOTexture.h:55
@ kOOTextureAllowRectTexture
Definition OOTexture.h:57
@ kOOTextureExtractChannelA
Definition OOTexture.h:70
@ kOOTextureExtractChannelB
Definition OOTexture.h:69
@ kOOTextureNoFNFMessage
Definition OOTexture.h:58
@ kOOTextureNoShrink
Definition OOTexture.h:53
@ kOOTextureMinFilterDefault
Definition OOTexture.h:45
@ kOOTextureExtractChannelNone
Definition OOTexture.h:66
@ kOOTextureMagFilterMask
Definition OOTexture.h:73
@ kOOTextureExtractChannelMask
Definition OOTexture.h:65
@ kOOTextureDefinedFlags
Definition OOTexture.h:78
@ kOOTextureMinFilterLinear
Definition OOTexture.h:47
@ kOOTextureAllowCubeMap
Definition OOTexture.h:61
@ kOOTextureMinFilterMask
Definition OOTexture.h:72
@ kOOTextureRepeatT
Definition OOTexture.h:56
@ kOOTextureDefaultOptions
Definition OOTexture.h:76
@ kOOTextureMinFilterMipMap
Definition OOTexture.h:48
@ kOOTextureMagFilterNearest
Definition OOTexture.h:50
@ kOOTextureExtraShrink
Definition OOTexture.h:54
@ kOOTextureExtractChannelG
Definition OOTexture.h:68
@ kOOTextureFlagsAllowedForRectangleTexture
Definition OOTexture.h:91
@ kOOTextureExtractChannelR
Definition OOTexture.h:67
@ kOOTextureMagFilterLinear
Definition OOTexture.h:51
@ kOOTextureMinFilterNearest
Definition OOTexture.h:46
#define kOOTextureDefaultAnisotropy
Definition OOTexture.h:97
NSString *const kOOTextureSpecifierBindingKey
Definition OOTexture.m:60
@ kOOTextureDataGrayscaleAlpha
Definition OOTexture.h:105
@ kOOTextureDataGrayscale
Definition OOTexture.h:104
@ kOOTextureDataInvalid
Definition OOTexture.h:101
@ kOOTextureDataRGBA
Definition OOTexture.h:103
NSString *const kOOTextureSpecifierIlluminationModeKey
Definition OOTexture.m:57
NSString *const kOOTextureSpecifierMinFilterKey
Definition OOTexture.m:46
NSString *const kOOTextureSpecifierMagFilterKey
Definition OOTexture.m:47
NSString *const kOOTextureSpecifierAnisotropyKey
Definition OOTexture.m:53
uint32_t OOTextureFlags
Definition OOTexture.h:95
NSString *const kOOTextureSpecifierRepeatSKey
Definition OOTexture.m:50
OOPixMapFormat OOTextureDataFormat
Definition OOTexture.h:107
#define kOOTextureDefaultLODBias
Definition OOTexture.h:98
NSString *const kOOTextureSpecifierModulateColorKey
Definition OOTexture.m:56
BOOL OOInterpretTextureSpecifier(id specifier, NSString **outName, OOTextureFlags *outOptions, float *outAnisotropy, float *outLODBias, BOOL ignoreExtract)
Definition OOTexture.m:648
NSString *const kOOTextureSpecifierCubeMapKey
Definition OOTexture.m:52
NSString *const kOOTextureSpecifierSwizzleKey
Definition OOTexture.m:45
NSString *const kOOTextureSpecifierExtraShrinkKey
Definition OOTexture.m:49
NSString *const kOOTextureSpecifierLODBiasKey
Definition OOTexture.m:54
#define CLEAR_TRACE_CONTEXT()
Definition OOTexture.m:128
#define SET_TRACE_CONTEXT(str)
Definition OOTexture.m:124
static OOCache * sRecentTextures
Definition OOTexture.m:94
@ kRecentTexturesCount
Definition OOTexture.m:89
static NSMutableDictionary * sLiveTextureCache
Definition OOTexture.m:92
NSString * OOTextureCacheKeyForSpecifier(id specifier)
Definition OOTexture.m:875
static BOOL sCheckedExtensions
Definition OOTexture.m:96
static NSMutableSet * sAllLiveTextures
Definition OOTexture.m:93
static NSString *const kOOTextureSpecifierFlagValueInternalKey
Definition OOTexture.m:63
NSString * OOGenerateTextureCacheKey(NSString *directory, NSString *name, OOTextureFlags options, float anisotropy, float lodBias)
Definition OOTexture.m:862
OOTextureInfo gOOTextureInfo
Definition OOTexture.m:97
#define UNIVERSE
Definition Universe.h:827
void setPruneThreshold:(unsigned threshold)
Definition OOCache.m:279
void setName:(NSString *name)
Definition OOCache.m:350
NSArray * objectsByAge()
Definition OOCache.m:355
void setAutoPrune:(BOOL flag)
Definition OOCache.m:294
void removeObjectForKey:(id key)
Definition OOCache.m:269
void setObject:forKey:(id value,[forKey] id key)
Definition OOCache.m:256
OONullTexture * sharedNullTexture()
BOOL versionIsAtLeastMajor:minor:(unsigned maj,[minor] unsigned min)
OOOpenGLExtensionManager * sharedManager()
BOOL haveExtension:(NSString *extension)
id textureWithConfiguration:extraOptions:(id configuration,[extraOptions] OOTextureFlags extraOptions)
Definition OOTexture.m:191
OOTexture * existingTextureForKey:(NSString *key)
Definition OOTexture.m:469
void checkExtensions()
Definition OOTexture.m:481
void apply()
Definition OOTexture.m:258
NSSize dimensions()
Definition OOTexture.m:292
NSSize originalDimensions()
Definition OOTexture.m:298
BOOL isCubeMap()
Definition OOTexture.m:319
BOOL isRectangleTexture()
Definition OOTexture.m:314
id textureWithGenerator:enqueue:(OOTextureGenerator *generator,[enqueue] BOOL enqueue)
Definition OOTexture.m:214
void applyNone()
Definition OOTexture.m:263
NSString * cacheKey()
Definition OOTexture.m:287
OOPixMap copyPixMapRepresentation()
Definition OOTexture.m:309
id textureWithName:inFolder:options:anisotropy:lodBias:(NSString *name,[inFolder] NSString *directory,[options] OOTextureFlags options,[anisotropy] GLfloat anisotropy,[lodBias] GLfloat lodBias)
Definition OOTexture.m:132
BOOL isMipMapped()
Definition OOTexture.m:303
void rebindAllTextures()
Definition OOTexture.m:350
GLint glTextureName()
Definition OOTexture.m:329
BOOL isFinishedLoading()
Definition OOTexture.m:282
void ensureFinishedLoading()
Definition OOTexture.m:278
void dealloc()
Definition OOTexture.m:251
id nullTexture()
Definition OOTexture.m:204
void clearCache()
Definition OOTexture.m:335
NSSize texCoordsScale()
Definition OOTexture.m:324
NSString * pathForFileNamed:inFolder:(NSString *fileName,[inFolder] NSString *folderName)
voidpf void uLong size
Definition ioapi.h:126