Skip to content

Commit f596a1e

Browse files
committed
More deltas to get hello world working on metal
1 parent 906e952 commit f596a1e

File tree

15 files changed

+169
-40
lines changed

15 files changed

+169
-40
lines changed

Ports/iOSPort/nativeSources/CN1METALTransform.m

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040
static int matrixStackTop = -1;
4141

4242
void CN1_Metal_InitMatrices(int framebufferWidth, int framebufferHeight) {
43+
// NSLog(@"CN1_Metal_InitMatrices called with width=%d, height=%d", framebufferWidth, framebufferHeight);
44+
4345
// Create orthographic projection for 2D UI rendering
4446
// Origin at top-left (0,0), Y increases downward (UIKit convention)
4547
CN1_Metal_ProjectionMatrix = CN1_Metal_MakeOrtho(
@@ -49,6 +51,12 @@ void CN1_Metal_InitMatrices(int framebufferWidth, int framebufferHeight) {
4951
);
5052
CN1_Metal_ProjectionMatrixVersion++;
5153

54+
// NSLog(@"Projection matrix after init: [%.6f %.6f %.6f %.6f] [%.6f %.6f %.6f %.6f] [%.6f %.6f %.6f %.6f] [%.6f %.6f %.6f %.6f]",
55+
// CN1_Metal_ProjectionMatrix.columns[0][0], CN1_Metal_ProjectionMatrix.columns[0][1], CN1_Metal_ProjectionMatrix.columns[0][2], CN1_Metal_ProjectionMatrix.columns[0][3],
56+
// CN1_Metal_ProjectionMatrix.columns[1][0], CN1_Metal_ProjectionMatrix.columns[1][1], CN1_Metal_ProjectionMatrix.columns[1][2], CN1_Metal_ProjectionMatrix.columns[1][3],
57+
// CN1_Metal_ProjectionMatrix.columns[2][0], CN1_Metal_ProjectionMatrix.columns[2][1], CN1_Metal_ProjectionMatrix.columns[2][2], CN1_Metal_ProjectionMatrix.columns[2][3],
58+
// CN1_Metal_ProjectionMatrix.columns[3][0], CN1_Metal_ProjectionMatrix.columns[3][1], CN1_Metal_ProjectionMatrix.columns[3][2], CN1_Metal_ProjectionMatrix.columns[3][3]);
59+
5260
// Initialize model-view and transform to identity
5361
CN1_Metal_ModelViewMatrix = matrix_identity_float4x4;
5462
CN1_Metal_ModelViewMatrixVersion++;

Ports/iOSPort/nativeSources/ClipRect.m

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,7 @@ -(void)execute {
287287

288288

289289
+(void)updateClipToScale {
290+
#ifndef CN1_USE_METAL
290291
if ( clipIsTexture ){
291292
return;
292293
}
@@ -296,7 +297,7 @@ +(void)updateClipToScale {
296297
//CN1Log(@"Updating clip to scale");
297298
glScissor(clipX, displayHeight - clipY - clipH, clipW, clipH);
298299
}
299-
300+
#endif
300301
}
301302

302303
#ifndef CN1_USE_ARC

Ports/iOSPort/nativeSources/CodenameOne_GLAppDelegate.m

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,11 @@ - (void)applicationWillResignActive:(UIApplication *)application
339339
- (void)applicationDidEnterBackground:(UIApplication *)application
340340
{
341341
#ifdef CN1_BLOCK_SCREENSHOTS_ON_ENTER_BACKGROUND
342+
#ifdef CN1_USE_METAL
343+
[[CodenameOne_GLViewController instance] metalView].hidden = YES;
344+
#else
342345
[[CodenameOne_GLViewController instance] eaglView].hidden = YES;
346+
#endif
343347
cn1IsHiddenInBackground = YES;
344348
#endif
345349
if(editingComponent != nil) {
@@ -365,7 +369,11 @@ - (void)applicationDidEnterBackground:(UIApplication *)application
365369

366370
- (void)applicationWillEnterForeground:(UIApplication *)application
367371
{ if (cn1IsHiddenInBackground) {
372+
#ifdef CN1_USE_METAL
373+
[[CodenameOne_GLViewController instance] metalView].hidden = NO;
374+
#else
368375
[[CodenameOne_GLViewController instance] eaglView].hidden = NO;
376+
#endif
369377
}
370378

371379
/*

Ports/iOSPort/nativeSources/CodenameOne_GLViewController.m

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2006,7 +2006,9 @@ -(void)updateCanvas:(BOOL)animated {
20062006
// Metal updates drawable size automatically via CAMetalLayer
20072007
int framebufferWidth = (int)([[self metalView] drawableSize].width);
20082008
int framebufferHeight = (int)([[self metalView] drawableSize].height);
2009-
CN1_Metal_InitMatrices(framebufferWidth, framebufferHeight);
2009+
if (framebufferWidth > 0 && framebufferHeight > 0) {
2010+
CN1_Metal_InitMatrices(framebufferWidth, framebufferHeight);
2011+
}
20102012
#else
20112013
[[self eaglView] updateFrameBufferSize:(int)self.view.bounds.size.width h:(int)self.view.bounds.size.height];
20122014
#endif
@@ -2177,9 +2179,17 @@ -(void)setScissorRect:(MTLScissorRect)rect enabled:(BOOL)enabled {
21772179
view.scissorRect = rect;
21782180
view.scissorEnabled = enabled;
21792181

2180-
// Apply scissor to the current encoder if it exists
2181-
// Note: The iOSPort version doesn't use currentEncoder, so we'll store the state
2182-
// and it will be applied when the next encoder is created
2182+
// If there's a current encoder, apply scissor immediately
2183+
if (view.currentEncoder != nil) {
2184+
if (enabled) {
2185+
[view.currentEncoder setScissorRect:rect];
2186+
} else {
2187+
// Disable scissor by setting it to full drawable size
2188+
CGSize drawableSize = [view drawableSize];
2189+
MTLScissorRect fullRect = {0, 0, (NSUInteger)drawableSize.width, (NSUInteger)drawableSize.height};
2190+
[view.currentEncoder setScissorRect:fullRect];
2191+
}
2192+
}
21832193
}
21842194
#else
21852195
EAGLView* lastFoundEaglView;
@@ -2236,9 +2246,12 @@ - (void)awakeFromNib
22362246

22372247
// Initialize Metal matrices
22382248
METALView *metalView = [self metalView];
2239-
int framebufferWidth = (int)metalView.drawableSize.width;
2240-
int framebufferHeight = (int)metalView.drawableSize.height;
2241-
CN1_Metal_InitMatrices(framebufferWidth, framebufferHeight);
2249+
CAMetalLayer *layer = (CAMetalLayer *)metalView.layer;
2250+
int framebufferWidth = (int)layer.drawableSize.width;
2251+
int framebufferHeight = (int)layer.drawableSize.height;
2252+
if (framebufferWidth > 0 && framebufferHeight > 0) {
2253+
CN1_Metal_InitMatrices(framebufferWidth, framebufferHeight);
2254+
}
22422255
#else
22432256
#ifdef USE_ES2
22442257
if (!cn1CompareMatrices(GLKMatrix4Identity, CN1transformMatrix)) {
@@ -2818,7 +2831,9 @@ -(void) viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id)coor
28182831
// Metal updates drawable size automatically
28192832
int framebufferWidth = (int)([[self metalView] drawableSize].width);
28202833
int framebufferHeight = (int)([[self metalView] drawableSize].height);
2821-
CN1_Metal_InitMatrices(framebufferWidth, framebufferHeight);
2834+
if (framebufferWidth > 0 && framebufferHeight > 0) {
2835+
CN1_Metal_InitMatrices(framebufferWidth, framebufferHeight);
2836+
}
28222837
#else
28232838
[[self eaglView] updateFrameBufferSize:(int)size.width h:(int)size.height];
28242839
[[self eaglView] deleteFramebuffer];
@@ -2865,7 +2880,9 @@ -(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOr
28652880
// Metal updates drawable size automatically
28662881
int framebufferWidth = (int)([[self metalView] drawableSize].width);
28672882
int framebufferHeight = (int)([[self metalView] drawableSize].height);
2868-
CN1_Metal_InitMatrices(framebufferWidth, framebufferHeight);
2883+
if (framebufferWidth > 0 && framebufferHeight > 0) {
2884+
CN1_Metal_InitMatrices(framebufferWidth, framebufferHeight);
2885+
}
28692886
#else
28702887
[[self eaglView] updateFrameBufferSize:(int)self.view.bounds.size.width h:(int)self.view.bounds.size.height];
28712888
[[self eaglView] deleteFramebuffer];
@@ -3031,7 +3048,6 @@ - (void)drawFrame:(CGRect)rect
30313048
}
30323049
}
30333050
#ifdef CN1_USE_METAL
3034-
METALView *metalView = [self metalView];
30353051
[metalView presentFramebuffer];
30363052
#else
30373053
GLErrorLog;
@@ -3135,6 +3151,7 @@ - (BOOL)validateProgram:(GLuint)prog
31353151
return TRUE;
31363152
}
31373153

3154+
#ifndef CN1_USE_METAL
31383155
- (BOOL)loadShaders
31393156
{
31403157
GLuint vertShader, fragShader;
@@ -3202,9 +3219,10 @@ - (BOOL)loadShaders
32023219
glDeleteShader(vertShader);
32033220
if (fragShader)
32043221
glDeleteShader(fragShader);
3205-
3222+
32063223
return TRUE;
32073224
}
3225+
#endif // CN1_USE_METAL
32083226

32093227

32103228
-(BOOL)isPaintFinished {

Ports/iOSPort/nativeSources/DrawImage.m

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,22 @@ -(void)execute {
129129
pipelineDescriptor.fragmentFunction = [library newFunctionWithName:@"textured_fragment"];
130130
pipelineDescriptor.colorAttachments[0].pixelFormat = MTLPixelFormatBGRA8Unorm;
131131

132+
// Configure vertex descriptor for textured shader
133+
MTLVertexDescriptor *vertexDescriptor = [[MTLVertexDescriptor alloc] init];
134+
// Position attribute (float2) at attribute 0
135+
vertexDescriptor.attributes[0].format = MTLVertexFormatFloat2;
136+
vertexDescriptor.attributes[0].offset = 0;
137+
vertexDescriptor.attributes[0].bufferIndex = 0;
138+
// TexCoord attribute (float2) at attribute 1
139+
vertexDescriptor.attributes[1].format = MTLVertexFormatFloat2;
140+
vertexDescriptor.attributes[1].offset = sizeof(float) * 2;
141+
vertexDescriptor.attributes[1].bufferIndex = 0;
142+
// Layout for buffer 0 (interleaved position + texCoord)
143+
vertexDescriptor.layouts[0].stride = sizeof(float) * 4;
144+
vertexDescriptor.layouts[0].stepRate = 1;
145+
vertexDescriptor.layouts[0].stepFunction = MTLVertexStepFunctionPerVertex;
146+
pipelineDescriptor.vertexDescriptor = vertexDescriptor;
147+
132148
// Enable blending for alpha
133149
pipelineDescriptor.colorAttachments[0].blendingEnabled = YES;
134150
pipelineDescriptor.colorAttachments[0].rgbBlendOperation = MTLBlendOperationAdd;

Ports/iOSPort/nativeSources/DrawRect.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ -(id)initWithArgs:(int)c a:(int)a xpos:(int)xpos ypos:(int)ypos w:(int)w h:(int)
103103
height = h;
104104
return self;
105105
}
106+
106107
#ifdef CN1_USE_METAL
107108
-(void)execute {
108109
// Metal rendering path - draws rectangle outline

Ports/iOSPort/nativeSources/DrawString.m

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ -(void)execute {
138138
// Metal rendering path
139139
id<MTLRenderCommandEncoder> encoder = [self makeRenderCommandEncoder];
140140
if (!encoder) {
141+
NSLog(@"DrawString: No encoder available!");
141142
return;
142143
}
143144

@@ -188,6 +189,11 @@ -(void)execute {
188189
#endif
189190
});
190191

192+
if (!pipelineState) {
193+
NSLog(@"DrawString: Pipeline state is nil!");
194+
return;
195+
}
196+
191197
[encoder setRenderPipelineState:pipelineState];
192198

193199
// Calculate text dimensions
@@ -286,6 +292,7 @@ -(void)execute {
286292

287293
// Draw rectangle as triangle strip
288294
[encoder drawPrimitives:MTLPrimitiveTypeTriangleStrip vertexStart:0 vertexCount:4];
295+
// NSLog(@"DrawString draw command issued for '%@'", str);
289296
}
290297

291298
#elif USE_ES2

Ports/iOSPort/nativeSources/ExecutableOp.m

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#import <mach/mach_host.h>
2828

2929

30+
#ifndef CN1_USE_METAL
3031
extern void logGlErrorAt(const char *f, int l) {
3132
GLenum err = glGetError();
3233
if(err != GL_NO_ERROR) {
@@ -46,6 +47,11 @@ extern void logGlErrorAt(const char *f, int l) {
4647
}
4748
}
4849
}
50+
#else
51+
extern void logGlErrorAt(const char *f, int l) {
52+
// No-op for Metal builds - use Metal validation layers instead
53+
}
54+
#endif
4955

5056
@implementation ExecutableOp
5157
static BOOL blockDrawing = NO;
@@ -142,7 +148,8 @@ -(void)applyClip:(id<MTLRenderCommandEncoder>)encoder {
142148
}
143149

144150
-(simd_float4x4)getMVPMatrix {
145-
return CN1_Metal_GetMVPMatrix();
151+
simd_float4x4 mvp = CN1_Metal_GetMVPMatrix();
152+
return mvp;
146153
}
147154

148155
-(simd_float4)colorToFloat4:(int)color alpha:(int)alpha {

Ports/iOSPort/nativeSources/FillPolygon.m

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,11 @@ -(id)initWithArgs:(JAVA_FLOAT*)xCoords y:(JAVA_FLOAT*)yCoords num:(int)num color
122122
}
123123
-(void)execute
124124
{
125+
#ifdef CN1_USE_METAL
126+
// TODO: Implement Metal polygon rendering
127+
// For now, just skip rendering to allow build to succeed
128+
NSLog(@"FillPolygon: Metal implementation not yet available");
129+
#else
125130
glUseProgram(getOGLProgram());
126131

127132
float alph = ((float)alpha)/255.0;
@@ -183,13 +188,14 @@ -(void)execute
183188

184189
glDisableVertexAttribArray(vertexCoordAtt);
185190
GLErrorLog;
186-
191+
187192
//glUseProgram(CN1activeProgram);
188193
//GLErrorLog;
189-
190-
194+
195+
191196
// ---------- end
192-
197+
#endif // CN1_USE_METAL
198+
193199

194200
}
195201

Ports/iOSPort/nativeSources/FillRect.m

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ -(void)execute {
117117
// Metal rendering path
118118
id<MTLRenderCommandEncoder> encoder = [self makeRenderCommandEncoder];
119119
if (!encoder) {
120+
NSLog(@"FillRect: No encoder available!");
120121
return;
121122
}
122123

@@ -132,6 +133,18 @@ -(void)execute {
132133
pipelineDescriptor.fragmentFunction = [library newFunctionWithName:@"solidColor_fragment"];
133134
pipelineDescriptor.colorAttachments[0].pixelFormat = MTLPixelFormatBGRA8Unorm;
134135

136+
// Configure vertex descriptor
137+
MTLVertexDescriptor *vertexDescriptor = [[MTLVertexDescriptor alloc] init];
138+
// Position attribute (float2)
139+
vertexDescriptor.attributes[0].format = MTLVertexFormatFloat2;
140+
vertexDescriptor.attributes[0].offset = 0;
141+
vertexDescriptor.attributes[0].bufferIndex = 0;
142+
// Layout for buffer 0
143+
vertexDescriptor.layouts[0].stride = sizeof(float) * 2;
144+
vertexDescriptor.layouts[0].stepRate = 1;
145+
vertexDescriptor.layouts[0].stepFunction = MTLVertexStepFunctionPerVertex;
146+
pipelineDescriptor.vertexDescriptor = vertexDescriptor;
147+
135148
// Enable blending for alpha
136149
pipelineDescriptor.colorAttachments[0].blendingEnabled = YES;
137150
pipelineDescriptor.colorAttachments[0].rgbBlendOperation = MTLBlendOperationAdd;
@@ -178,6 +191,7 @@ -(void)execute {
178191

179192
// Draw rectangle as triangle strip
180193
[encoder drawPrimitives:MTLPrimitiveTypeTriangleStrip vertexStart:0 vertexCount:4];
194+
// NSLog(@"FillRect draw command issued");
181195
}
182196

183197
#elif USE_ES2

0 commit comments

Comments
 (0)