@@ -88,6 +88,10 @@ CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
88
88
#import " CCTextureCache.h"
89
89
#import " CCSpriteFrame.h"
90
90
91
+ #if __CC_METAL_SUPPORTED_AND_ENABLED
92
+ #import " CCMetalSupport_Private.h"
93
+ #endif
94
+
91
95
92
96
// CLASS IMPLEMENTATIONS:
93
97
@@ -113,7 +117,7 @@ -(BOOL)isKindOfClass:(Class)aClass {return [_target isKindOfClass:aClass];}
113
117
114
118
// Make concrete implementations for CCTexture methods commonly called at runtime.
115
119
-(GLuint)name {return [(CCTexture *)_target name ];}
116
- -(CGFloat)contentScale {return [_target contentScale ];}
120
+ -(CGFloat)contentScale {return [(CCTexture *) _target contentScale ];}
117
121
-(CGSize)contentSize {return [_target contentSize ];}
118
122
-(NSUInteger )pixelWidth {return [_target pixelWidth ];}
119
123
-(NSUInteger )pixelHeight {return [_target pixelHeight ];}
@@ -122,7 +126,7 @@ -(CCSpriteFrame *)createSpriteFrame {return [_target createSpriteFrame];}
122
126
123
127
// Make concrete implementations for CCSpriteFrame methods commonly called at runtime.
124
128
-(CGRect)rect {return [_target rect ];}
125
- -(CGPoint)offset {return [_target offset ];}
129
+ -(CGPoint)offset {return [(CCSpriteFrame *) _target offset ];}
126
130
-(BOOL )rotated {return [_target rotated ];}
127
131
-(CGSize)originalSize {return [_target originalSize ];}
128
132
-(CCTexture *)texture {return [_target texture ];}
@@ -152,7 +156,18 @@ - (void)dealloc
152
156
153
157
@implementation CCTexture
154
158
{
155
- CCProxy __weak *_proxy;
159
+ GLuint _name;
160
+ CGSize _sizeInPixels;
161
+ CGFloat _contentScale;
162
+ NSUInteger _width, _height;
163
+ CCTexturePixelFormat _format;
164
+ GLfloat _maxS, _maxT;
165
+ BOOL _premultipliedAlpha;
166
+ BOOL _hasMipmaps;
167
+
168
+ BOOL _antialiased;
169
+
170
+ CCProxy __weak *_proxy;
156
171
}
157
172
158
173
@synthesize contentSizeInPixels = _sizeInPixels, pixelFormat = _format, pixelWidth = _width, pixelHeight = _height, name = _name, maxS = _maxS, maxT = _maxT;
@@ -187,6 +202,42 @@ - (id) initWithData:(const void*)data pixelFormat:(CCTexturePixelFormat)pixelFor
187
202
{
188
203
if ((self = [super init ])) {
189
204
CCRenderDispatch (NO , ^{
205
+ #if __CC_METAL_SUPPORTED_AND_ENABLED
206
+ if ([CCConfiguration sharedConfiguration ].graphicsAPI == CCGraphicsAPIMetal){
207
+ id <MTLDevice > device = [CCMetalContext currentContext ].device ;
208
+
209
+ MTLSamplerDescriptor *samplerDesc = [MTLSamplerDescriptor new ];
210
+ samplerDesc.minFilter = MTLSamplerMinMagFilterLinear ;
211
+ samplerDesc.magFilter = MTLSamplerMinMagFilterLinear ;
212
+ samplerDesc.mipFilter = MTLSamplerMipFilterNotMipmapped ;
213
+ samplerDesc.sAddressMode = MTLSamplerAddressModeClampToEdge ;
214
+ samplerDesc.tAddressMode = MTLSamplerAddressModeClampToEdge ;
215
+
216
+ _metalSampler = [device newSamplerStateWithDescriptor: samplerDesc];
217
+
218
+ static const MTLPixelFormat metalFormats[] = {
219
+ MTLPixelFormatRGBA8Unorm ,
220
+ MTLPixelFormatInvalid , // CCTexturePixelFormat_RGB888,
221
+ MTLPixelFormatInvalid , // CCTexturePixelFormat_RGB565,
222
+ MTLPixelFormatA8Unorm , // CCTexturePixelFormat_A8,
223
+ MTLPixelFormatRG8Unorm , // CCTexturePixelFormat_I8,
224
+ MTLPixelFormatInvalid , // CCTexturePixelFormat_AI88,
225
+ MTLPixelFormatABGR4Unorm, // CCTexturePixelFormat_RGBA4444,
226
+ MTLPixelFormatInvalid , // CCTexturePixelFormat_RGB5A1,
227
+ MTLPixelFormatPVRTC_RGBA_4BPP, // CCTexturePixelFormat_PVRTC4,
228
+ MTLPixelFormatPVRTC_RGBA_2BPP, // CCTexturePixelFormat_PVRTC2,
229
+ };
230
+ MTLPixelFormat metalFormat = metalFormats[pixelFormat];
231
+ NSAssert (metalFormat != MTLPixelFormatInvalid , @" This texture format is not supported by Apple's Metal API." );
232
+
233
+ MTLTextureDescriptor *textureDesc = [MTLTextureDescriptor texture2DDescriptorWithPixelFormat: metalFormat width: width height: height mipmapped: NO ];
234
+ _metalTexture = [device newTextureWithDescriptor: textureDesc];
235
+
236
+ NSUInteger bytesPerRow = width*[CCTexture bitsPerPixelForFormat: pixelFormat]/8 ;
237
+ [_metalTexture replaceRegion: MTLRegionMake2D (0 , 0 , width, height) mipmapLevel: 0 withBytes: data bytesPerRow: bytesPerRow];
238
+ } else
239
+ #endif
240
+ {
190
241
CCGL_DEBUG_PUSH_GROUP_MARKER (" CCTexture: Init" );
191
242
192
243
// XXX: 32 bits or POT textures uses UNPACK of 4 (is this correct ??? )
@@ -233,6 +284,7 @@ - (id) initWithData:(const void*)data pixelFormat:(CCTexturePixelFormat)pixelFor
233
284
}
234
285
235
286
CCGL_DEBUG_POP_GROUP_MARKER ();
287
+ }
236
288
});
237
289
238
290
_sizeInPixels = sizeInPixels;
@@ -296,7 +348,7 @@ - (void*) keepData:(void*)data length:(NSUInteger)length
296
348
- (void ) dealloc
297
349
{
298
350
CCLOGINFO (@" cocos2d: deallocing %@ " , self);
299
-
351
+
300
352
GLuint name = _name;
301
353
if (name){
302
354
CCRenderDispatch (YES , ^{
@@ -618,6 +670,8 @@ +(void) PVRImagesHavePremultipliedAlpha:(BOOL)haveAlphaPremultiplied
618
670
#pragma mark -
619
671
#pragma mark CCTexture2D - GLFilter
620
672
673
+ #warning Not implemented for Metal.
674
+
621
675
//
622
676
// Use to apply MIN/MAG filter
623
677
//
0 commit comments