Skip to content

Commit ea3ba67

Browse files
committed
Fixing some Metal runtime validation errors.
1 parent 7ce3e77 commit ea3ba67

File tree

5 files changed

+36
-35
lines changed

5 files changed

+36
-35
lines changed

cocos2d/CCRenderTexture.m

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,15 @@ - (id)initWithWidth:(int)w height:(int)h pixelFormat:(CCTexturePixelFormat)forma
114114
-(id)initWithWidth:(int)width height:(int)height pixelFormat:(CCTexturePixelFormat) format depthStencilFormat:(GLuint)depthStencilFormat
115115
{
116116
if((self = [super init])){
117-
NSAssert(format != CCTexturePixelFormat_A8, @"only RGB and RGBA formats are valid for a render texture");
117+
#if __CC_METAL_SUPPORTED_AND_ENABLED
118+
if([CCConfiguration sharedConfiguration].graphicsAPI == CCGraphicsAPIMetal){
119+
NSAssert(format == CCTexturePixelFormat_RGBA8888, @"Only RGBA8 pixel formats are supported for Metal render textures. (The internally created texture is actually BRGA8)");
120+
format = CCTexturePixelFormat_BGRA8888;
121+
} else
122+
#endif
123+
{
124+
NSAssert(format != CCTexturePixelFormat_A8, @"only RGB and RGBA formats are valid for a render texture");
125+
}
118126

119127
CCDirector *director = [CCDirector sharedDirector];
120128

cocos2d/CCRenderer.m

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -317,13 +317,15 @@ -(void)prepareWithProjection:(const GLKMatrix4 *)projection framebuffer:(CCFrame
317317

318318
for(NSString *name in _globalShaderUniforms){
319319
NSValue *value = _globalShaderUniforms[name];
320-
size_t bytes = value.CCRendererSizeOf;
321320

322-
void * buff = CCGraphicsBufferPushElements(uniformBuffer, bytes);
321+
// Round up to the next multiple of 16 since Metal types have an alignment of 16 bytes at most.
322+
size_t alignedBytes = ((value.CCRendererSizeOf - 1) | 0xF) + 1;
323+
324+
void * buff = CCGraphicsBufferPushElements(uniformBuffer, alignedBytes);
323325
[value getValue:buff];
324326
offsets[name] = @(offset);
325327

326-
offset += bytes;
328+
offset += alignedBytes;
327329
}
328330

329331
_globalShaderUniformBufferOffsets = offsets;

cocos2d/CCShader.m

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,9 @@ -(instancetype)initWithGLProgram:(GLuint)program uniformSetters:(NSDictionary *)
415415
// If both args are active, they must match.
416416
NSCAssert(!vertexArg || !fragmentArg || vertexArg.bufferDataSize == fragmentArg.bufferDataSize, @"Vertex and fragment argument type don't match for '%@'.", vertexArg.name);
417417

418+
// Round up to the next multiple of 16 since Metal types have an alignment of 16 bytes at most.
419+
size_t alignedBytes = ((bytes - 1) | 0xF) + 1;
420+
418421
return ^(CCRenderer *renderer, NSDictionary *shaderUniforms, NSDictionary *globalShaderUniforms){
419422
CCGraphicsBufferMetal *uniformBuffer = (CCGraphicsBufferMetal *)renderer->_buffers->_uniformBuffer;
420423
id<MTLBuffer> metalBuffer = uniformBuffer->_buffer;
@@ -424,7 +427,7 @@ -(instancetype)initWithGLProgram:(GLuint)program uniformSetters:(NSDictionary *)
424427
NSValue *value = shaderUniforms[name];
425428
if(value){
426429
// Try finding a per-node value first and append it to the uniform buffer.
427-
void *buff = CCGraphicsBufferPushElements(uniformBuffer, bytes);
430+
void *buff = CCGraphicsBufferPushElements(uniformBuffer, alignedBytes);
428431
[value getValue:buff];
429432

430433
offset = buff - uniformBuffer->_ptr;
@@ -550,6 +553,8 @@ -(instancetype)initWithMetalVertexFunction:(id<MTLFunction>)vertexFunction fragm
550553
-(instancetype)initWithMetalVertexShaderSource:(NSString *)vertexSource fragmentShaderSource:(NSString *)fragmentSource
551554
{
552555
CCMetalContext *context = [CCMetalContext currentContext];
556+
557+
// TODO this is a terrible terrible hack.
553558
NSString *header = CC_METAL(
554559
using namespace metal;
555560

@@ -577,7 +582,6 @@ -(instancetype)initWithMetalVertexShaderSource:(NSString *)vertexSource fragment
577582
float4 cosTime;
578583
float4 random01;
579584
} CCGlobalUniforms;
580-
581585
);
582586

583587
id<MTLFunction> vertexFunction = nil;

cocos2d/CCTexture.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,9 @@ typedef NS_ENUM(NSUInteger, CCTexturePixelFormat) {
105105
///! 2-bit PVRTC-compressed texture: PVRTC2
106106
CCTexturePixelFormat_PVRTC2,
107107

108+
///! 32-bit texture: BGRA8888
109+
CCTexturePixelFormat_BGRA8888,
110+
108111
///! Default texture format: RGBA8888
109112
CCTexturePixelFormat_Default = CCTexturePixelFormat_RGBA8888,
110113
};

cocos2d/CCTexture.m

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
103103
MTLPixelFormatInvalid, //CCTexturePixelFormat_RGB5A1,
104104
MTLPixelFormatPVRTC_RGBA_4BPP, //CCTexturePixelFormat_PVRTC4,
105105
MTLPixelFormatPVRTC_RGBA_2BPP, //CCTexturePixelFormat_PVRTC2,
106+
MTLPixelFormatBGRA8Unorm,
106107
};
107108

108109
#endif
@@ -824,47 +825,29 @@ +(CCTexturePixelFormat) defaultAlphaPixelFormat
824825

825826
+(NSUInteger) bitsPerPixelForFormat:(CCTexturePixelFormat)format
826827
{
827-
NSUInteger ret=0;
828-
829828
switch (format) {
830829
case CCTexturePixelFormat_RGBA8888:
831-
ret = 32;
832-
break;
833-
case CCTexturePixelFormat_RGB888:
830+
case CCTexturePixelFormat_BGRA8888:
834831
// It is 32 and not 24, since its internal representation uses 32 bits.
835-
ret = 32;
836-
break;
832+
case CCTexturePixelFormat_RGB888:
833+
return 32;
837834
case CCTexturePixelFormat_RGB565:
838-
ret = 16;
839-
break;
840835
case CCTexturePixelFormat_RGBA4444:
841-
ret = 16;
842-
break;
843836
case CCTexturePixelFormat_RGB5A1:
844-
ret = 16;
845-
break;
846837
case CCTexturePixelFormat_AI88:
847-
ret = 16;
848-
break;
838+
return 16;
849839
case CCTexturePixelFormat_A8:
850-
ret = 8;
851-
break;
852840
case CCTexturePixelFormat_I8:
853-
ret = 8;
854-
break;
841+
return 8;
855842
case CCTexturePixelFormat_PVRTC4:
856-
ret = 4;
857-
break;
843+
return 4;
858844
case CCTexturePixelFormat_PVRTC2:
859-
ret = 2;
860-
break;
845+
return 2;
861846
default:
862-
ret = -1;
863847
NSAssert1(NO , @"bitsPerPixelForFormat: %ld, unrecognised pixel format", (long)format);
864848
CCLOG(@"bitsPerPixelForFormat: %ld, cannot give useful result", (long)format);
865-
break;
849+
return -1;
866850
}
867-
return ret;
868851
}
869852

870853
-(NSUInteger) bitsPerPixelForFormat
@@ -906,13 +889,14 @@ -(NSString*) stringForFormat
906889
case CCTexturePixelFormat_PVRTC2:
907890
return @"PVRTC2";
908891

892+
case CCTexturePixelFormat_BGRA8888:
893+
return @"BGRA8888";
894+
909895
default:
910896
NSAssert1(NO , @"stringForFormat: %ld, unrecognised pixel format", (long)_format);
911897
CCLOG(@"stringForFormat: %ld, cannot give useful result", (long)_format);
912-
break;
898+
return nil;
913899
}
914-
915-
return nil;
916900
}
917901
@end
918902

0 commit comments

Comments
 (0)