Skip to content

Commit 8445bc1

Browse files
committed
Revert "Removing NPOT checks since it’s required by GLES2 and has been universally supported by Macs since at least 2007."
This reverts commit 1bd2e11.
1 parent 5d4fdb7 commit 8445bc1

File tree

6 files changed

+71
-6
lines changed

6 files changed

+71
-6
lines changed

cocos2d/CCConfiguration.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ typedef NS_ENUM(NSUInteger, CCDevice) {
7575

7676
GLint _maxTextureSize;
7777
BOOL _supportsPVRTC;
78+
BOOL _supportsNPOT;
7879
BOOL _supportsBGRA8888;
7980
BOOL _supportsDiscardFramebuffer;
8081
BOOL _supportsShareableVAO;
@@ -91,6 +92,12 @@ typedef NS_ENUM(NSUInteger, CCDevice) {
9192
*/
9293
@property (nonatomic, readonly) GLint maxTextureUnits;
9394

95+
/** Whether or not the GPU supports NPOT (Non Power Of Two) textures.
96+
OpenGL ES 2.0 already supports NPOT (iOS).
97+
98+
*/
99+
@property (nonatomic, readonly) BOOL supportsNPOT;
100+
94101
/** Whether or not PVR Texture Compressed is supported */
95102
@property (nonatomic, readonly) BOOL supportsPVRTC;
96103

cocos2d/CCConfiguration.m

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ @implementation CCConfiguration
4444

4545
@synthesize maxTextureSize = _maxTextureSize, maxTextureUnits=_maxTextureUnits;
4646
@synthesize supportsPVRTC = _supportsPVRTC;
47+
@synthesize supportsNPOT = _supportsNPOT;
4748
@synthesize supportsBGRA8888 = _supportsBGRA8888;
4849
@synthesize supportsDiscardFramebuffer = _supportsDiscardFramebuffer;
4950
@synthesize supportsShareableVAO = _supportsShareableVAO;
@@ -181,6 +182,12 @@ -(void) getOpenGLvariables
181182
glGetIntegerv(GL_MAX_TEXTURE_SIZE, &_maxTextureSize);
182183
glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &_maxTextureUnits );
183184

185+
#ifdef __CC_PLATFORM_IOS
186+
_supportsNPOT = YES;
187+
#elif defined(__CC_PLATFORM_MAC)
188+
_supportsNPOT = [self checkForGLExtension:@"GL_ARB_texture_non_power_of_two"];
189+
#endif
190+
184191
_supportsPVRTC = [self checkForGLExtension:@"GL_IMG_texture_compression_pvrtc"];
185192

186193
// It seems that somewhere between firmware iOS 3.0 and 4.2 Apple renamed
@@ -215,6 +222,14 @@ -(GLint) maxTextureUnits
215222
return _maxTextureUnits;
216223
}
217224

225+
-(BOOL) supportsNPOT
226+
{
227+
if( ! _openGLInitialized )
228+
[self getOpenGLvariables];
229+
230+
return _supportsNPOT;
231+
}
232+
218233
-(BOOL) supportsPVRTC
219234
{
220235
if( ! _openGLInitialized )
@@ -293,6 +308,7 @@ -(void) dumpInfo
293308
printf("cocos2d: GL_MAX_SAMPLES: %d\n", _maxSamplesAllowed);
294309
printf("cocos2d: GL supports PVRTC: %s\n", (_supportsPVRTC ? "YES" : "NO") );
295310
printf("cocos2d: GL supports BGRA8888 textures: %s\n", (_supportsBGRA8888 ? "YES" : "NO") );
311+
printf("cocos2d: GL supports NPOT textures: %s\n", (_supportsNPOT ? "YES" : "NO") );
296312
printf("cocos2d: GL supports discard_framebuffer: %s\n", (_supportsDiscardFramebuffer ? "YES" : "NO") );
297313
printf("cocos2d: GL supports shareable VAO: %s\n", (_supportsShareableVAO ? "YES" : "NO") );
298314

cocos2d/CCEffectRenderer.m

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,27 @@ - (BOOL)setupGLResourcesWithSize:(CGSize)size
5151

5252
glPushGroupMarkerEXT(0, "CCEffectRenderTarget: allocateRenderTarget");
5353

54+
// Textures may need to be a power of two
55+
NSUInteger powW;
56+
NSUInteger powH;
57+
58+
if( [[CCConfiguration sharedConfiguration] supportsNPOT] )
59+
{
60+
powW = size.width;
61+
powH = size.height;
62+
}
63+
else
64+
{
65+
powW = CCNextPOT(size.width);
66+
powH = CCNextPOT(size.height);
67+
}
68+
5469
static const CCTexturePixelFormat kRenderTargetDefaultPixelFormat = CCTexturePixelFormat_RGBA8888;
5570
static const float kRenderTargetDefaultContentScale = 1.0f;
5671

5772
// Create a new texture object for use as the color attachment of the new
5873
// FBO.
59-
_texture = [[CCTexture alloc] initWithData:nil pixelFormat:kRenderTargetDefaultPixelFormat pixelsWide:size.width pixelsHigh:size.height contentSizeInPixels:size contentScale:kRenderTargetDefaultContentScale];
74+
_texture = [[CCTexture alloc] initWithData:nil pixelFormat:kRenderTargetDefaultPixelFormat pixelsWide:powW pixelsHigh:powH contentSizeInPixels:size contentScale:kRenderTargetDefaultContentScale];
6075
[_texture setAliasTexParameters];
6176

6277
// Save the old FBO binding so it can be restored after we create the new

cocos2d/CCRenderTexture.m

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,21 @@ -(void)create
157157

158158
glGetIntegerv(GL_FRAMEBUFFER_BINDING, &_oldFBO);
159159

160-
void *data = calloc(pixelW*pixelH, 4);
161-
CCTexture *texture = [[CCTexture alloc] initWithData:data pixelFormat:_pixelFormat pixelsWide:pixelW pixelsHigh:pixelH contentSizeInPixels:CGSizeMake(pixelW, pixelH) contentScale:_contentScale];
160+
// textures must be power of two
161+
NSUInteger powW;
162+
NSUInteger powH;
163+
164+
if( [[CCConfiguration sharedConfiguration] supportsNPOT] ) {
165+
powW = pixelW;
166+
powH = pixelH;
167+
} else {
168+
powW = CCNextPOT(pixelW);
169+
powH = CCNextPOT(pixelH);
170+
}
171+
172+
void *data = calloc(powW*powH, 4);
173+
174+
CCTexture *texture = [[CCTexture alloc] initWithData:data pixelFormat:_pixelFormat pixelsWide:powW pixelsHigh:powH contentSizeInPixels:CGSizeMake(pixelW, pixelH) contentScale:_contentScale];
162175
self.texture = texture;
163176

164177
free(data);
@@ -179,7 +192,7 @@ -(void)create
179192
//create and attach depth buffer
180193
glGenRenderbuffers(1, &depthRenderBuffer);
181194
glBindRenderbuffer(GL_RENDERBUFFER, depthRenderBuffer);
182-
glRenderbufferStorage(GL_RENDERBUFFER, _depthStencilFormat, (GLsizei)pixelW, (GLsizei)pixelH);
195+
glRenderbufferStorage(GL_RENDERBUFFER, _depthStencilFormat, (GLsizei)powW, (GLsizei)powH);
183196
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthRenderBuffer);
184197

185198
// if depth format is the one with stencil part, bind same render buffer as stencil attachment

cocos2d/CCTexture.m

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -399,8 +399,16 @@ - (id) initWithCGImage:(CGImageRef)cgImage contentScale:(CGFloat)contentScale
399399
pixelFormat = CCTexturePixelFormat_A8;
400400
}
401401

402-
textureWidth = CGImageGetWidth(cgImage);
403-
textureHeight = CGImageGetHeight(cgImage);
402+
if( ! [conf supportsNPOT] )
403+
{
404+
textureWidth = CCNextPOT(CGImageGetWidth(cgImage));
405+
textureHeight = CCNextPOT(CGImageGetHeight(cgImage));
406+
}
407+
else
408+
{
409+
textureWidth = CGImageGetWidth(cgImage);
410+
textureHeight = CGImageGetHeight(cgImage);
411+
}
404412

405413
#ifdef __CC_PLATFORM_IOS
406414

cocos2d/CCTexturePVR.m

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,12 @@ - (BOOL)unpackPVRv2Data:(unsigned char*)data PVRLen:(NSUInteger)len
299299
if( flipped )
300300
CCLOGWARN(@"cocos2d: WARNING: Image is flipped. Regenerate it using PVRTexTool");
301301

302+
if( ! [configuration supportsNPOT] &&
303+
( header->width != CCNextPOT(header->width) || header->height != CCNextPOT(header->height ) ) ) {
304+
CCLOGWARN(@"cocos2d: ERROR: Loding an NPOT texture (%dx%d) but is not supported on this device", header->width, header->height);
305+
return NO;
306+
}
307+
302308
for( NSUInteger i=0; i < (unsigned int)PVR2_MAX_TABLE_ELEMENTS ; i++) {
303309
if( v2_pixel_formathash[i].pixelFormat == formatFlags ) {
304310

0 commit comments

Comments
 (0)