Skip to content

Commit 906e952

Browse files
committed
Initial work on Metal support.
Hello world sort of works with this, but not perfectly. There are still clipping issues, and probably performance issues. Only implements a few of the key executable ops - enough to get hello world working.
1 parent 9548141 commit 906e952

17 files changed

+1288
-33
lines changed

Ports/iOSPort/nativeSources/CN1ES2compat.h

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,56 @@
2020
* Please contact Codename One through http://www.codenameone.com/ if you
2121
* need additional information or have any questions.
2222
*/
23+
//#define CN1_USE_METAL
24+
25+
#ifndef CN1_USE_METAL
2326
#define USE_ES2 1
27+
#endif
28+
2429
enum CN1GLenum {
2530
CN1_GL_ALPHA_TEXTURE,
2631
CN1_GL_VERTEX_COLORS
2732
};
2833

29-
#ifdef USE_ES2
34+
#ifdef CN1_USE_METAL
35+
// Metal compatibility layer
36+
// These macros are no-ops for Metal since it uses a different rendering model
37+
// Transform operations are handled by CN1METALTransform instead
38+
#import "CN1METALTransform.h"
39+
40+
#define _glMatrixMode(foo) ((void)0)
41+
#define _glLoadIdentity() CN1_Metal_LoadIdentity()
42+
#define _glOrthof(p1,p2,p3,p4,p5,p6) ((void)0)
43+
#define _glDisable(foo) ((void)0)
44+
#define _glEnable(foo) ((void)0)
45+
#define _glScalef(xScale,yScale,zScale) CN1_Metal_Scale(xScale,yScale,zScale)
46+
#define _glTranslatef(x,y,z) CN1_Metal_Translate(x,y,z)
47+
#define _glColor4f(r,g,b,a) ((void)0)
48+
#define _glEnableClientState(s) ((void)0)
49+
#define _glDisableClientState(s) ((void)0)
50+
#define _glTexCoordPointer(size,type,stride,pointer) ((void)0)
51+
#define _glVertexPointer(size,type,stride,pointer) ((void)0)
52+
#define _glDrawArrays(mode,first,count) ((void)0)
53+
#define _glRotatef(angle,x,y,z) CN1_Metal_Rotate(angle,x,y,z)
54+
#define _glEnableCN1State(state) ((void)0)
55+
#define _glDisableCN1State(state) ((void)0)
56+
#define _glAlphaMaskTexCoordPointer(size,type,stride,pointer) ((void)0)
57+
58+
// Define GL constants as no-ops for Metal
59+
#define GL_TEXTURE_2D 0
60+
#define GL_BLEND 0
61+
#define GL_ONE_MINUS_SRC_ALPHA 0
62+
#define GL_SRC_ALPHA 0
63+
#define GL_MODELVIEW 0
64+
#define GL_PROJECTION 0
65+
#define GL_VERTEX_ARRAY 0
66+
#define GL_TEXTURE_COORD_ARRAY 0
67+
#define GL_TRIANGLES 0
68+
#define GL_TRIANGLE_FAN 0
69+
#define GL_TRIANGLE_STRIP 0
70+
#define GL_NO_ERROR 0
71+
72+
#elif USE_ES2
3073
#import <GLKit/GLKit.h>
3174
#import <OpenGLES/ES2/gl.h>
3275
#import "ExecutableOp.h"

Ports/iOSPort/nativeSources/ClipRect.m

Lines changed: 74 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
#import "ClipRect.h"
2424
#import "CodenameOne_GLViewController.h"
2525
#import "FillRect.h"
26+
#ifdef CN1_USE_METAL
27+
#import <Metal/Metal.h>
28+
#endif
2629
#ifdef USE_ES2
2730
#import "DrawTextureAlphaMask.h"
2831
#import "FillPolygon.h"
@@ -92,7 +95,74 @@ -(void)executeWithLog {
9295
}
9396

9497
-(void)execute {
95-
#ifdef USE_ES2
98+
#ifdef CN1_USE_METAL
99+
// Metal implementation using scissor rectangle
100+
// For polygon/texture clipping, we'd need stencil buffer - not implemented yet
101+
if (texture != 0 || numPoints > 0) {
102+
NSLog(@"ClipRect: Polygon/texture clipping not yet implemented in Metal");
103+
return;
104+
}
105+
106+
clipIsTexture = NO;
107+
int x2 = x + width;
108+
int y2 = y + height;
109+
int orX = drawingRect.origin.x;
110+
int orY = drawingRect.origin.y;
111+
if(x < orX) {
112+
x = orX;
113+
width = x2 - x;
114+
}
115+
if(y < orY) {
116+
y = orY;
117+
height = y2 - y;
118+
}
119+
int destX2 = (int)(drawingRect.origin.x + drawingRect.size.width);
120+
int destY2 = (int)(drawingRect.origin.y + drawingRect.size.height);
121+
if(x2 > destX2) {
122+
width = destX2 - x;
123+
}
124+
if(y2 > destY2) {
125+
height = destY2 - y;
126+
}
127+
128+
if(width > 0 && height > 0) {
129+
[super clipBlock:NO];
130+
int scale = scaleValue;
131+
int displayHeight = [CodenameOne_GLViewController instance].view.bounds.size.height * scale;
132+
133+
// Check if this is full screen - if so, disable scissor
134+
if(width == [CodenameOne_GLViewController instance].view.bounds.size.width * scale && height == displayHeight) {
135+
MTLScissorRect fullScreenRect = {0, 0, 0, 0};
136+
[[CodenameOne_GLViewController instance] setScissorRect:fullScreenRect enabled:NO];
137+
return;
138+
}
139+
140+
// Set scissor rectangle for Metal
141+
clipX = x;
142+
clipW = width;
143+
if (clipX < 0) {
144+
clipX = 0;
145+
clipW = width;
146+
}
147+
148+
clipY = y;
149+
clipH = height;
150+
if (clipY < 0) {
151+
clipY = 0;
152+
clipH = height;
153+
}
154+
155+
[ClipRect updateClipToScale];
156+
157+
// Apply scissor to Metal encoder
158+
MTLScissorRect scissor = {clipX, clipY, clipW, clipH};
159+
[[CodenameOne_GLViewController instance] setScissorRect:scissor enabled:YES];
160+
161+
clipApplied = YES;
162+
} else {
163+
[super clipBlock:YES];
164+
}
165+
#elif defined(USE_ES2)
96166
if ( texture != 0 || numPoints > 0 ){
97167
clipX = x; clipY=y; clipW=width; clipH=height;
98168
glClearStencil(0x0);
@@ -131,8 +201,9 @@ -(void)execute {
131201
return;
132202
}
133203

134-
204+
135205
#endif
206+
#ifndef CN1_USE_METAL
136207
clipIsTexture = NO;
137208
int x2 = x + width;
138209
int y2 = y + height;
@@ -210,6 +281,7 @@ -(void)execute {
210281
#endif
211282
clipApplied = NO;
212283
}
284+
#endif // #ifndef CN1_USE_METAL
213285

214286
}
215287

Ports/iOSPort/nativeSources/CodenameOne_GLViewController.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,17 @@
2323
#import "CN1ES2compat.h"
2424
#import <UIKit/UIKit.h>
2525

26+
#ifdef CN1_USE_METAL
27+
#import "METALView.h"
28+
#else
2629
#import <OpenGLES/EAGL.h>
2730
#import "EAGLView.h"
2831
#import <OpenGLES/ES1/gl.h>
2932
#import <OpenGLES/ES1/glext.h>
3033
#import <OpenGLES/ES2/gl.h>
3134
#import <OpenGLES/ES2/glext.h>
35+
#endif
36+
3237
#import "ExecutableOp.h"
3338
#import "PaintOp.h"
3439
#import "GLUIImage.h"
@@ -159,7 +164,11 @@
159164
#define CN1_CAP_ROUND 1
160165
#define CN1_CAP_SQUARE 2
161166

167+
#ifdef CN1_USE_METAL
168+
#define METALVIEW [[CodenameOne_GLViewController instance] metalView]
169+
#else
162170
#define EAGLVIEW [[CodenameOne_GLViewController instance] eaglView]
171+
#endif
163172

164173
//ADD_INCLUDE
165174

@@ -191,8 +200,10 @@ MFMessageComposeViewControllerDelegate, CLLocationManagerDelegate, AVAudioRecord
191200
#endif
192201
> {
193202
@private
203+
#ifndef CN1_USE_METAL
194204
EAGLContext *context;
195205
GLuint program;
206+
#endif
196207

197208
BOOL animating;
198209
NSInteger animationFrameInterval;
@@ -227,7 +238,13 @@ MFMessageComposeViewControllerDelegate, CLLocationManagerDelegate, AVAudioRecord
227238
- (void)signIn:(GIDSignIn *)signIn didDisconnectWithUser:(GIDGoogleUser *)user withError:(NSError *)error;
228239
#endif
229240

241+
#ifdef CN1_USE_METAL
242+
-(METALView*)metalView;
243+
-(void)setScissorRect:(MTLScissorRect)rect enabled:(BOOL)enabled;
244+
#else
230245
-(EAGLView*)eaglView;
246+
#endif
247+
231248
-(void)startAnimation;
232249
-(void)stopAnimation;
233250
+(BOOL)isDrawTextureSupported;

0 commit comments

Comments
 (0)