Skip to content

Commit 9548141

Browse files
committed
Initial metal foundations
1 parent daa53ee commit 9548141

File tree

5 files changed

+543
-31
lines changed

5 files changed

+543
-31
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright (c) 2012, Codename One and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
* This code is free software; you can redistribute it and/or modify it
5+
* under the terms of the GNU General Public License version 2 only, as
6+
* published by the Free Software Foundation. Codename One designates this
7+
* particular file as subject to the "Classpath" exception as provided
8+
* by Oracle in the LICENSE file that accompanied this code.
9+
*
10+
* This code is distributed in the hope that it will be useful, but WITHOUT
11+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13+
* version 2 for more details (a copy is included in the LICENSE file that
14+
* accompanied this code).
15+
*
16+
* You should have received a copy of the GNU General Public License version
17+
* 2 along with this work; if not, write to the Free Software Foundation,
18+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19+
*
20+
* Please contact Codename One through http://www.codenameone.com/ if you
21+
* need additional information or have any questions.
22+
*/
23+
24+
#ifndef CN1METALTransform_h
25+
#define CN1METALTransform_h
26+
27+
#ifdef CN1_USE_METAL
28+
#import <Foundation/Foundation.h>
29+
@import simd;
30+
31+
// Global matrix state for Metal rendering
32+
extern simd_float4x4 CN1_Metal_ProjectionMatrix;
33+
extern simd_float4x4 CN1_Metal_ModelViewMatrix;
34+
extern simd_float4x4 CN1_Metal_TransformMatrix;
35+
36+
// Version tracking for matrix changes (optimization)
37+
extern int CN1_Metal_ProjectionMatrixVersion;
38+
extern int CN1_Metal_ModelViewMatrixVersion;
39+
extern int CN1_Metal_TransformMatrixVersion;
40+
41+
// Initialize matrices for 2D rendering
42+
void CN1_Metal_InitMatrices(int framebufferWidth, int framebufferHeight);
43+
44+
// Set matrices
45+
void CN1_Metal_SetProjectionMatrix(simd_float4x4 matrix);
46+
void CN1_Metal_SetModelViewMatrix(simd_float4x4 matrix);
47+
void CN1_Metal_SetTransformMatrix(simd_float4x4 matrix);
48+
49+
// Get combined MVP matrix (Projection * ModelView * Transform)
50+
simd_float4x4 CN1_Metal_GetMVPMatrix(void);
51+
52+
// Transform operations
53+
void CN1_Metal_Translate(float x, float y, float z);
54+
void CN1_Metal_Scale(float x, float y, float z);
55+
void CN1_Metal_Rotate(float angle, float x, float y, float z);
56+
57+
// Matrix stack operations (for nested transforms)
58+
void CN1_Metal_PushMatrix(void);
59+
void CN1_Metal_PopMatrix(void);
60+
void CN1_Metal_LoadIdentity(void);
61+
62+
// Helper functions
63+
simd_float4x4 CN1_Metal_MakeOrtho(float left, float right, float bottom, float top, float near, float far);
64+
simd_float4x4 CN1_Metal_MakeTranslation(float x, float y, float z);
65+
simd_float4x4 CN1_Metal_MakeScale(float x, float y, float z);
66+
simd_float4x4 CN1_Metal_MakeRotation(float angle, float x, float y, float z);
67+
68+
#endif // CN1_USE_METAL
69+
#endif // CN1METALTransform_h
Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
/*
2+
* Copyright (c) 2012, Codename One and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
* This code is free software; you can redistribute it and/or modify it
5+
* under the terms of the GNU General Public License version 2 only, as
6+
* published by the Free Software Foundation. Codename One designates this
7+
* particular file as subject to the "Classpath" exception as provided
8+
* by Oracle in the LICENSE file that accompanied this code.
9+
*
10+
* This code is distributed in the hope that it will be useful, but WITHOUT
11+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13+
* version 2 for more details (a copy is included in the LICENSE file that
14+
* accompanied this code).
15+
*
16+
* You should have received a copy of the GNU General Public License version
17+
* 2 along with this work; if not, write to the Free Software Foundation,
18+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19+
*
20+
* Please contact Codename One through http://www.codenameone.com/ if you
21+
* need additional information or have any questions.
22+
*/
23+
24+
#ifdef CN1_USE_METAL
25+
#import "CN1METALTransform.h"
26+
@import simd;
27+
28+
// Global matrix state
29+
simd_float4x4 CN1_Metal_ProjectionMatrix;
30+
simd_float4x4 CN1_Metal_ModelViewMatrix;
31+
simd_float4x4 CN1_Metal_TransformMatrix;
32+
33+
int CN1_Metal_ProjectionMatrixVersion = 0;
34+
int CN1_Metal_ModelViewMatrixVersion = 0;
35+
int CN1_Metal_TransformMatrixVersion = 0;
36+
37+
// Matrix stack for nested transforms (max depth 32)
38+
#define MAX_MATRIX_STACK_DEPTH 32
39+
static simd_float4x4 matrixStack[MAX_MATRIX_STACK_DEPTH];
40+
static int matrixStackTop = -1;
41+
42+
void CN1_Metal_InitMatrices(int framebufferWidth, int framebufferHeight) {
43+
// Create orthographic projection for 2D UI rendering
44+
// Origin at top-left (0,0), Y increases downward (UIKit convention)
45+
CN1_Metal_ProjectionMatrix = CN1_Metal_MakeOrtho(
46+
0, framebufferWidth,
47+
framebufferHeight, 0, // Flipped Y for UIKit coordinates
48+
-1, 1
49+
);
50+
CN1_Metal_ProjectionMatrixVersion++;
51+
52+
// Initialize model-view and transform to identity
53+
CN1_Metal_ModelViewMatrix = matrix_identity_float4x4;
54+
CN1_Metal_ModelViewMatrixVersion++;
55+
56+
CN1_Metal_TransformMatrix = matrix_identity_float4x4;
57+
CN1_Metal_TransformMatrixVersion++;
58+
59+
// Reset matrix stack
60+
matrixStackTop = -1;
61+
}
62+
63+
void CN1_Metal_SetProjectionMatrix(simd_float4x4 matrix) {
64+
CN1_Metal_ProjectionMatrix = matrix;
65+
CN1_Metal_ProjectionMatrixVersion++;
66+
}
67+
68+
void CN1_Metal_SetModelViewMatrix(simd_float4x4 matrix) {
69+
CN1_Metal_ModelViewMatrix = matrix;
70+
CN1_Metal_ModelViewMatrixVersion++;
71+
}
72+
73+
void CN1_Metal_SetTransformMatrix(simd_float4x4 matrix) {
74+
CN1_Metal_TransformMatrix = matrix;
75+
CN1_Metal_TransformMatrixVersion++;
76+
}
77+
78+
simd_float4x4 CN1_Metal_GetMVPMatrix(void) {
79+
// Combine matrices: Projection * ModelView * Transform
80+
return simd_mul(CN1_Metal_ProjectionMatrix,
81+
simd_mul(CN1_Metal_ModelViewMatrix,
82+
CN1_Metal_TransformMatrix));
83+
}
84+
85+
void CN1_Metal_Translate(float x, float y, float z) {
86+
simd_float4x4 translation = CN1_Metal_MakeTranslation(x, y, z);
87+
CN1_Metal_TransformMatrix = simd_mul(CN1_Metal_TransformMatrix, translation);
88+
CN1_Metal_TransformMatrixVersion++;
89+
}
90+
91+
void CN1_Metal_Scale(float x, float y, float z) {
92+
simd_float4x4 scale = CN1_Metal_MakeScale(x, y, z);
93+
CN1_Metal_TransformMatrix = simd_mul(CN1_Metal_TransformMatrix, scale);
94+
CN1_Metal_TransformMatrixVersion++;
95+
}
96+
97+
void CN1_Metal_Rotate(float angle, float x, float y, float z) {
98+
simd_float4x4 rotation = CN1_Metal_MakeRotation(angle, x, y, z);
99+
CN1_Metal_TransformMatrix = simd_mul(CN1_Metal_TransformMatrix, rotation);
100+
CN1_Metal_TransformMatrixVersion++;
101+
}
102+
103+
void CN1_Metal_PushMatrix(void) {
104+
if (matrixStackTop < MAX_MATRIX_STACK_DEPTH - 1) {
105+
matrixStackTop++;
106+
matrixStack[matrixStackTop] = CN1_Metal_TransformMatrix;
107+
} else {
108+
NSLog(@"CN1METALTransform: Matrix stack overflow!");
109+
}
110+
}
111+
112+
void CN1_Metal_PopMatrix(void) {
113+
if (matrixStackTop >= 0) {
114+
CN1_Metal_TransformMatrix = matrixStack[matrixStackTop];
115+
matrixStackTop--;
116+
CN1_Metal_TransformMatrixVersion++;
117+
} else {
118+
NSLog(@"CN1METALTransform: Matrix stack underflow!");
119+
}
120+
}
121+
122+
void CN1_Metal_LoadIdentity(void) {
123+
CN1_Metal_TransformMatrix = matrix_identity_float4x4;
124+
CN1_Metal_TransformMatrixVersion++;
125+
}
126+
127+
// Helper: Create orthographic projection matrix
128+
simd_float4x4 CN1_Metal_MakeOrtho(float left, float right, float bottom, float top, float near, float far) {
129+
simd_float4x4 m = matrix_identity_float4x4;
130+
m.columns[0][0] = 2.0f / (right - left);
131+
m.columns[1][1] = 2.0f / (top - bottom);
132+
m.columns[2][2] = -2.0f / (far - near);
133+
m.columns[3][0] = -(right + left) / (right - left);
134+
m.columns[3][1] = -(top + bottom) / (top - bottom);
135+
m.columns[3][2] = -(far + near) / (far - near);
136+
m.columns[3][3] = 1.0f;
137+
return m;
138+
}
139+
140+
// Helper: Create translation matrix
141+
simd_float4x4 CN1_Metal_MakeTranslation(float x, float y, float z) {
142+
simd_float4x4 m = matrix_identity_float4x4;
143+
m.columns[3][0] = x;
144+
m.columns[3][1] = y;
145+
m.columns[3][2] = z;
146+
return m;
147+
}
148+
149+
// Helper: Create scale matrix
150+
simd_float4x4 CN1_Metal_MakeScale(float x, float y, float z) {
151+
simd_float4x4 m = matrix_identity_float4x4;
152+
m.columns[0][0] = x;
153+
m.columns[1][1] = y;
154+
m.columns[2][2] = z;
155+
return m;
156+
}
157+
158+
// Helper: Create rotation matrix around arbitrary axis
159+
simd_float4x4 CN1_Metal_MakeRotation(float angleRadians, float x, float y, float z) {
160+
// Normalize axis
161+
float length = sqrtf(x * x + y * y + z * z);
162+
if (length == 0) {
163+
return matrix_identity_float4x4;
164+
}
165+
x /= length;
166+
y /= length;
167+
z /= length;
168+
169+
float c = cosf(angleRadians);
170+
float s = sinf(angleRadians);
171+
float t = 1.0f - c;
172+
173+
simd_float4x4 m = matrix_identity_float4x4;
174+
m.columns[0][0] = t * x * x + c;
175+
m.columns[0][1] = t * x * y + s * z;
176+
m.columns[0][2] = t * x * z - s * y;
177+
178+
m.columns[1][0] = t * x * y - s * z;
179+
m.columns[1][1] = t * y * y + c;
180+
m.columns[1][2] = t * y * z + s * x;
181+
182+
m.columns[2][0] = t * x * z + s * y;
183+
m.columns[2][1] = t * y * z - s * x;
184+
m.columns[2][2] = t * z * z + c;
185+
186+
return m;
187+
}
188+
189+
#endif // CN1_USE_METAL

Ports/iOSPort/nativeSources/METALView.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@
4040
GLuint defaultFramebuffer, colorRenderbuffer;
4141

4242
}
43-
@property (nonatomic, retain) MTLCommandQueue* commandQueue;
44-
@property (nonatomic, retain) MTLCommandBuffer* commandBuffer;
45-
@property (nonatomic, retain) MTLRenderPassDescriptor* renderPassDescriptor;
46-
@property (nonatomic, retain) MTLRenderCommandEncoder* renderCommandEncoder;
47-
@property (nonatomic, retain) MTLDrawable* drawable;
48-
@property (nonatomic, retain) UIView* peerComponentsLayer;
43+
@property (nonatomic, strong) id<MTLDevice> device;
44+
@property (nonatomic, strong) id<MTLCommandQueue> commandQueue;
45+
@property (nonatomic, strong) id<MTLCommandBuffer> commandBuffer;
46+
@property (nonatomic, strong) MTLRenderPassDescriptor* renderPassDescriptor;
47+
@property (nonatomic, strong) id<CAMetalDrawable> drawable;
48+
@property (nonatomic, strong) UIView* peerComponentsLayer;
4949

5050
-(void)textViewDidChange:(UITextView *)textView;
5151
-(void)deleteFramebuffer;
@@ -57,5 +57,6 @@
5757
-(void) keyboardNextClicked;
5858
-(void) addPeerComponent:(UIView*) view;
5959
-(void) removePeerComponent:(UIView*) view;
60+
-(id<MTLRenderCommandEncoder>)makeRenderCommandEncoder;
6061
@end
6162
#endif

0 commit comments

Comments
 (0)