@aguacerowx/react-native 0.0.38 → 0.0.41

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,1107 +0,0 @@
1
- import Foundation
2
- import MapboxMaps
3
- import Metal
4
- import libzstd
5
- import simd
6
-
7
- // MARK: - Swift-only wrapper for CustomLayerHost conformance
8
- @objc internal final class GridRenderLayerHost: NSObject, CustomLayerHost {
9
- weak var layer: GridRenderLayer?
10
-
11
- init(layer: GridRenderLayer) {
12
- self.layer = layer
13
- super.init()
14
- }
15
-
16
- func renderingWillStart(_ metalDevice: MTLDevice, colorPixelFormat: UInt, depthStencilPixelFormat: UInt) {
17
- layer?.internalRenderingWillStart(metalDevice, colorPixelFormat: colorPixelFormat, depthStencilPixelFormat: depthStencilPixelFormat)
18
- }
19
-
20
- func render(_ parameters: CustomLayerRenderParameters, mtlCommandBuffer: MTLCommandBuffer, mtlRenderPassDescriptor: MTLRenderPassDescriptor) {
21
- layer?.internalRender(parameters, mtlCommandBuffer: mtlCommandBuffer, mtlRenderPassDescriptor: mtlRenderPassDescriptor)
22
- }
23
-
24
- func renderingWillEnd() {
25
- layer?.internalRenderingWillEnd()
26
- }
27
- }
28
-
29
- @objc(GridRenderLayer)
30
- public class GridRenderLayer: NSObject {
31
-
32
- // MARK: - Properties
33
-
34
- public var id: String
35
- @objc internal var hostWrapper: GridRenderLayerHost!
36
-
37
- // Metal objects
38
- private var device: MTLDevice!
39
- private var commandQueue: MTLCommandQueue!
40
- private var pipelineState: MTLRenderPipelineState!
41
- private var vertexBuffer: MTLBuffer?
42
- private var indexBuffer: MTLBuffer?
43
- private var dataTexture: MTLTexture?
44
- private var colormapTexture: MTLTexture?
45
- private var dataSamplerState: MTLSamplerState!
46
- private var colormapSamplerState: MTLSamplerState!
47
- private var isDataSamplerLinear: Bool = false
48
-
49
- private var pendingColormapUpdate: String?
50
- private var pendingDataUpdate: (data: String, nx: NSNumber, ny: NSNumber, scale: NSNumber, offset: NSNumber, missing: NSNumber, scaleType: String)?
51
- private var pendingGeometryUpdate: (corners: [String: Any], gridDef: [String: Any])?
52
-
53
- // Layer state
54
- private var indexCount: Int = 0
55
- private var uniforms = FragmentUniforms(
56
- opacity: 1.0,
57
- dataRange: SIMD2<Float>(0.0, 1.0),
58
- scale: 1.0,
59
- offset: 0.0,
60
- missingQuantized: 127.0,
61
- textureSize: SIMD2<Float>(0.0, 0.0),
62
- smoothing: 1,
63
- scaleType: 0,
64
- isPtype: 0,
65
- isMRMS: 0
66
- )
67
- private var isVisible = false
68
- private var pendingActiveFrameKey: String?
69
- private var lastRequestedCacheKey: String?
70
- private let inspectorCache = InspectorDataCache.shared
71
- private struct FrameMetadata {
72
- let scale: Float
73
- let offset: Float
74
- let missing: Float
75
- let scaleType: Int
76
- let nx: Float
77
- let ny: Float
78
- let filePath: String
79
- let originalScale: Float
80
- let originalOffset: Float
81
- }
82
- private var frameCache: [String: FrameMetadata] = [:]
83
- private let frameCacheQueue = DispatchQueue(label: "com.aguacero.frame-cache-queue")
84
- private let frameProcessingQueue = DispatchQueue(label: "com.aguacero.frame-processing", qos: .userInitiated, attributes: .concurrent)
85
- private let semaphore = DispatchSemaphore(value: 8)
86
-
87
- private struct VertexInfo {
88
- var mercX: Float
89
- var mercY: Float
90
- var texU: Float
91
- var texV: Float
92
- var index: UInt16
93
- }
94
-
95
- // MARK: - Performance Helper
96
- private func logPerf(_ msg: String) {
97
- var taskInfo = mach_task_basic_info()
98
- var count = mach_msg_type_number_t(MemoryLayout<mach_task_basic_info>.size)/4
99
- let kerr: kern_return_t = withUnsafeMutablePointer(to: &taskInfo) {
100
- $0.withMemoryRebound(to: integer_t.self, capacity: 1) {
101
- task_info(mach_task_self_, task_flavor_t(MACH_TASK_BASIC_INFO), $0, &count)
102
- }
103
- }
104
- let mem = (kerr == KERN_SUCCESS) ? Float(taskInfo.resident_size) / 1024.0 / 1024.0 : 0
105
- print("⚡️ [PERF] [GridRenderLayer] \(msg) | RAM: \(String(format: "%.2f", mem)) MB")
106
- }
107
-
108
- @objc
109
- public init(id: String) {
110
- self.id = id
111
- super.init()
112
- self.hostWrapper = GridRenderLayerHost(layer: self)
113
- }
114
-
115
- @objc public func getHostWrapper() -> Any {
116
- return self.hostWrapper as Any
117
- }
118
-
119
- // MARK: - Public Methods (called from Manager)
120
-
121
- @objc public func setOpacity(value: Float) {
122
- self.uniforms.opacity = value
123
- NotificationCenter.default.post(name: NSNotification.Name("TriggerMapRepaint"), object: nil)
124
- }
125
-
126
- @objc public func setDataRange(value: [NSNumber]) {
127
- self.uniforms.dataRange = SIMD2<Float>(value[0].floatValue, value[1].floatValue)
128
- NotificationCenter.default.post(name: NSNotification.Name("TriggerMapRepaint"), object: nil)
129
- }
130
-
131
- @objc public func setSmoothing(value: Bool) {
132
- self.uniforms.smoothing = value ? 1 : 0
133
- NotificationCenter.default.post(name: NSNotification.Name("TriggerMapRepaint"), object: nil)
134
- }
135
-
136
- @objc(setIsMRMSWithIsMRMS:)
137
- public func setIsMRMS(isMRMS: Bool) {
138
- self.uniforms.isMRMS = isMRMS ? 1 : 0
139
- NotificationCenter.default.post(name: NSNotification.Name("TriggerMapRepaint"), object: nil)
140
- }
141
-
142
- // ADD THIS METHOD
143
- @objc(setVariableWithVariable:)
144
- public func setVariable(variable: String) {
145
- let isPtypeVar = (variable == "ptypeRefl" || variable == "ptypeRate")
146
- self.uniforms.isPtype = isPtypeVar ? 1 : 0
147
- NotificationCenter.default.post(name: NSNotification.Name("TriggerMapRepaint"), object: nil)
148
- }
149
-
150
- @objc public func clear() {
151
- self.isVisible = false
152
- self.inspectorCache.clear()
153
- }
154
-
155
- @objc public func updateDataParameters(scale: NSNumber, offset: NSNumber, missing: NSNumber, scaleType: NSNumber) { // ADD scaleType parameter
156
- // Update both the inspector cache AND the rendering uniforms
157
- self.uniforms.scale = scale.floatValue
158
- self.uniforms.offset = offset.floatValue
159
- self.uniforms.missingQuantized = missing.floatValue
160
- self.uniforms.scaleType = Int32(scaleType.intValue) // ADD THIS
161
-
162
- inspectorCache.update(
163
- data: nil,
164
- nx: inspectorCache.nx,
165
- ny: inspectorCache.ny,
166
- scale: scale.floatValue,
167
- offset: offset.floatValue,
168
- missing: missing.floatValue,
169
- scaleType: scaleType.intValue
170
- )
171
- }
172
-
173
- @objc public func updateColormapTexture(colormapAsBase64: String) {
174
- guard let device = self.device else {
175
- pendingColormapUpdate = colormapAsBase64
176
- return
177
- }
178
- guard let data = Data(base64Encoded: colormapAsBase64) else { return }
179
- let textureDescriptor = MTLTextureDescriptor()
180
- textureDescriptor.pixelFormat = .rgba8Unorm
181
- textureDescriptor.width = 256
182
- textureDescriptor.height = 1
183
- textureDescriptor.usage = .shaderRead
184
- guard let texture = device.makeTexture(descriptor: textureDescriptor) else { return }
185
- texture.replace(
186
- region: MTLRegionMake2D(0, 0, 256, 1),
187
- mipmapLevel: 0,
188
- withBytes: (data as NSData).bytes,
189
- bytesPerRow: 256 * 4
190
- )
191
- self.colormapTexture = texture
192
- }
193
-
194
- @objc(clearGpuCache)
195
- public func clearGpuCache() {
196
- print("ℹ️ [GridRenderLayer] clearGpuCache called")
197
- frameProcessingQueue.async { [weak self] in
198
- guard let self = self else { return }
199
- self.frameCacheQueue.async {
200
- self.frameCache.removeAll()
201
- }
202
- }
203
- }
204
-
205
- private func reconstructAndTransformInPlace(data: inout Data) {
206
- let count = data.count
207
- if count == 0 { return }
208
-
209
- // Get raw pointer to memory (Bypasses Swift Array bounds checking)
210
- data.withUnsafeMutableBytes { rawBuffer in
211
- guard let ptr = rawBuffer.baseAddress?.assumingMemoryBound(to: UInt8.self) else { return }
212
-
213
- // 1. Setup first byte
214
- // Cast to Int8 to handle the delta logic, then back to UInt8 for storage
215
- var runningValue: Int8 = Int8(bitPattern: ptr[0])
216
-
217
- // Transform first byte immediately: runningValue + 128
218
- // We use &+ to allow overflow wrapping (which is exactly what we want for -128->0 mapping)
219
- ptr[0] = UInt8(bitPattern: runningValue) &+ 128
220
-
221
- // 2. Optimized Loop (One pass for everything)
222
- for i in 1..<count {
223
- let delta = Int8(bitPattern: ptr[i])
224
-
225
- // Reconstruction: Add delta to previous value
226
- runningValue = runningValue &+ delta
227
-
228
- // Transformation: Add 128 to shift into UInt8 range for Texture
229
- ptr[i] = UInt8(bitPattern: runningValue) &+ 128
230
- }
231
- }
232
- }
233
-
234
- private func processRawData(fileData: Data) -> Data? {
235
- let start = CFAbsoluteTimeGetCurrent()
236
-
237
- // 1. Decompress
238
- // This allocates the only buffer we will use.
239
- let t1 = CFAbsoluteTimeGetCurrent()
240
- guard var workingData = self.decompressZstd(data: fileData) else {
241
- print("❌ [GridRenderLayer] Failed to decompress zstd data")
242
- return nil
243
- }
244
- let t1_diff = (CFAbsoluteTimeGetCurrent() - t1) * 1000
245
-
246
- // 2. Reconstruct AND Transform (In-Place)
247
- // No new memory allocation here.
248
- let t2 = CFAbsoluteTimeGetCurrent()
249
- self.reconstructAndTransformInPlace(data: &workingData)
250
- let t2_diff = (CFAbsoluteTimeGetCurrent() - t2) * 1000
251
-
252
- // 3. (Legacy Timer Placeholder to match logs)
253
- // Since we combined them, this is effectively instant now
254
- let t3_diff = 0.0
255
-
256
- let total = (CFAbsoluteTimeGetCurrent() - start) * 1000
257
-
258
- // Log CPU transformation metrics
259
- // Expect Recon+Trans to drop from ~1000ms to ~10-20ms
260
- logPerf("PROCESS RAW: Total \(String(format: "%.1f", total))ms (Zstd: \(String(format: "%.1f", t1_diff))ms, Recon+Trans: \(String(format: "%.1f", t2_diff))ms)")
261
-
262
- return workingData
263
- }
264
-
265
- private func createTextureFromBytes(bytes: Data, nx: Int, ny: Int) -> MTLTexture? {
266
- guard let device = self.device else { return nil }
267
- let textureDescriptor = MTLTextureDescriptor()
268
- textureDescriptor.pixelFormat = .r8Unorm
269
- textureDescriptor.width = nx
270
- textureDescriptor.height = ny
271
- textureDescriptor.usage = .shaderRead
272
-
273
- guard let texture = device.makeTexture(descriptor: textureDescriptor) else { return nil }
274
- texture.replace(
275
- region: MTLRegionMake2D(0, 0, nx, ny),
276
- mipmapLevel: 0,
277
- withBytes: (bytes as NSData).bytes,
278
- bytesPerRow: nx
279
- )
280
- return texture
281
- }
282
-
283
- private func updateInspectorCache(filePath: String, reconstructedData: Data?, nx: Int, ny: Int, scale: Float, offset: Float, missing: Float, scaleType: Int) {
284
- self.inspectorCache.updateWithFilePath(
285
- filePath: filePath,
286
- nx: nx,
287
- ny: ny,
288
- scale: scale,
289
- offset: offset,
290
- missing: missing,
291
- scaleType: scaleType
292
- )
293
- }
294
-
295
- @objc(setActiveFrameWithCacheKey:)
296
- public func setActiveFrame(cacheKey: String) {
297
- logPerf("setActiveFrame requested: \(cacheKey)")
298
- setActiveFrameInternal(cacheKey: cacheKey, isExternalRequest: true)
299
- }
300
-
301
- private func setActiveFrameInternal(cacheKey: String, isExternalRequest: Bool) {
302
- if isExternalRequest {
303
- lastRequestedCacheKey = cacheKey
304
- } else {
305
- if lastRequestedCacheKey != cacheKey { return }
306
- }
307
-
308
- var frameResult: FrameMetadata?
309
- frameCacheQueue.sync {
310
- frameResult = frameCache[cacheKey]
311
- }
312
-
313
- if let frame = frameResult {
314
- frameProcessingQueue.async { [weak self] in
315
- guard let self = self else { return }
316
- if self.lastRequestedCacheKey != cacheKey { return }
317
-
318
- // ALWAYS load from disk. It is fast enough now (20ms).
319
- // No caching needed.
320
- let diskStart = CFAbsoluteTimeGetCurrent()
321
- guard let fileData = try? Data(contentsOf: URL(fileURLWithPath: frame.filePath)) else {
322
- print("❌ [GridRenderLayer] Failed to load file: \(frame.filePath)")
323
- return
324
- }
325
-
326
- // processRawData is now optimized (in-place) and takes ~20ms
327
- guard let finalTextureBytes = self.processRawData(fileData: fileData) else {
328
- print("❌ [GridRenderLayer] Failed to process data for: \(cacheKey)")
329
- return
330
- }
331
-
332
- // Final check before jumping to main thread
333
- if self.lastRequestedCacheKey != cacheKey { return }
334
-
335
- DispatchQueue.main.async { [weak self] in
336
- // ... (Keep existing Main Thread texture upload logic) ...
337
- guard let self = self else { return }
338
-
339
- if self.lastRequestedCacheKey != cacheKey { return }
340
-
341
- let nx = Int(frame.nx)
342
- let ny = Int(frame.ny)
343
-
344
- if let existingTexture = self.dataTexture,
345
- existingTexture.width == nx,
346
- existingTexture.height == ny {
347
- existingTexture.replace(
348
- region: MTLRegionMake2D(0, 0, nx, ny),
349
- mipmapLevel: 0,
350
- withBytes: (finalTextureBytes as NSData).bytes,
351
- bytesPerRow: nx
352
- )
353
- } else {
354
- guard let texture = self.createTextureFromBytes(bytes: finalTextureBytes, nx: nx, ny: ny) else { return }
355
- self.dataTexture = texture
356
- }
357
-
358
- self.uniforms.scale = frame.scale
359
- self.uniforms.offset = frame.offset
360
- self.uniforms.missingQuantized = frame.missing
361
- self.uniforms.textureSize = SIMD2<Float>(frame.nx, frame.ny)
362
- self.uniforms.scaleType = Int32(frame.scaleType)
363
-
364
- self.isVisible = true
365
- self.pendingActiveFrameKey = nil
366
- NotificationCenter.default.post(name: NSNotification.Name("TriggerMapRepaint"), object: nil)
367
-
368
- // Update inspector in background
369
- self.frameProcessingQueue.async { [weak self] in
370
- self?.updateInspectorCache(
371
- filePath: frame.filePath,
372
- reconstructedData: finalTextureBytes, // Pass the data we already processed!
373
- nx: nx,
374
- ny: ny,
375
- scale: frame.scale,
376
- offset: frame.offset,
377
- missing: frame.missing,
378
- scaleType: frame.scaleType
379
- )
380
- }
381
- }
382
- }
383
- } else {
384
- if isExternalRequest {
385
- self.pendingActiveFrameKey = cacheKey
386
- }
387
- }
388
- }
389
-
390
- @objc(primeGpuCacheWithFrameInfo:)
391
- public func primeGpuCache(frameInfo: [String: [String: Any]]) {
392
- // logPerf("primeGpuCache called with \(frameInfo.count) items")
393
-
394
- // We do NOT need the frameProcessingQueue here anymore because we are just storing metadata.
395
- // Doing this synchronously on the caller thread (which is a background thread from Manager) is fine and much faster.
396
-
397
- self.frameCacheQueue.async { [weak self] in
398
- guard let self = self else { return }
399
-
400
- for (cacheKey, info) in frameInfo {
401
- if self.frameCache[cacheKey] != nil { continue }
402
-
403
- guard let filePath = info["filePath"] as? String,
404
- let nx = info["nx"] as? NSNumber,
405
- let ny = info["ny"] as? NSNumber,
406
- let scale = info["scale"] as? NSNumber,
407
- let offset = info["offset"] as? NSNumber,
408
- let missing = info["missing"] as? NSNumber,
409
- let scaleTypeStr = info["scaleType"] as? String,
410
- let originalScale = info["originalScale"] as? NSNumber,
411
- let originalOffset = info["originalOffset"] as? NSNumber
412
- else { continue }
413
-
414
- let metadata = FrameMetadata(
415
- scale: scale.floatValue,
416
- offset: offset.floatValue,
417
- missing: missing.floatValue,
418
- scaleType: (scaleTypeStr == "sqrt") ? 1 : 0,
419
- nx: nx.floatValue,
420
- ny: ny.floatValue,
421
- filePath: filePath,
422
- originalScale: originalScale.floatValue,
423
- originalOffset: originalOffset.floatValue
424
- // processedData: nil <--- REMOVED
425
- )
426
-
427
- self.frameCache[cacheKey] = metadata
428
- }
429
- }
430
- }
431
-
432
- @objc public func updateDataTexture(data: String, nx: NSNumber, ny: NSNumber, scale: NSNumber, offset: NSNumber, missing: NSNumber, scaleType: String) {
433
- let filePath = data
434
- logPerf("updateDataTexture called for \(filePath)")
435
-
436
- frameProcessingQueue.async { [weak self] in
437
- guard let self = self else { return }
438
-
439
- guard let fileData = try? Data(contentsOf: URL(fileURLWithPath: filePath)),
440
- let finalTextureBytes = self.processRawData(fileData: fileData)
441
- else {
442
- print("❌ [GridRenderLayer] FAST LANE: Failed to process initial frame data.")
443
- return
444
- }
445
-
446
- DispatchQueue.main.async { [weak self] in
447
- guard let self = self else { return }
448
-
449
- let nxInt = nx.intValue
450
- let nyInt = ny.intValue
451
-
452
- // REUSE TEXTURE if dimensions match
453
- if let existingTexture = self.dataTexture,
454
- existingTexture.width == nxInt,
455
- existingTexture.height == nyInt {
456
- existingTexture.replace(
457
- region: MTLRegionMake2D(0, 0, nxInt, nyInt),
458
- mipmapLevel: 0,
459
- withBytes: (finalTextureBytes as NSData).bytes,
460
- bytesPerRow: nxInt
461
- )
462
- } else {
463
- // Create new texture only if needed
464
- guard let texture = self.createTextureFromBytes(bytes: finalTextureBytes, nx: nxInt, ny: nyInt) else {
465
- print("❌ [GridRenderLayer] FAST LANE: Failed to create texture")
466
- return
467
- }
468
- self.dataTexture = texture
469
- }
470
-
471
- self.uniforms.scale = scale.floatValue
472
- self.uniforms.offset = offset.floatValue
473
- self.uniforms.missingQuantized = missing.floatValue
474
- self.uniforms.scaleType = Int32((scaleType == "sqrt") ? 1 : 0)
475
- self.uniforms.textureSize = SIMD2<Float>(nx.floatValue, ny.floatValue)
476
-
477
- self.isVisible = true
478
- NotificationCenter.default.post(name: NSNotification.Name("TriggerMapRepaint"), object: nil)
479
-
480
- self.updateInspectorCache(
481
- filePath: filePath,
482
- reconstructedData: nil,
483
- nx: nx.intValue,
484
- ny: ny.intValue,
485
- scale: scale.floatValue,
486
- offset: offset.floatValue,
487
- missing: missing.floatValue,
488
- scaleType: (scaleType == "sqrt") ? 1 : 0
489
- )
490
- }
491
- }
492
- }
493
-
494
- @objc public func updateGeometry(corners: [String: Any], gridDef: [String: Any]) {
495
- guard self.device != nil else {
496
- print("⚠️ [GridRenderLayer] Device not ready yet, storing geometry for later")
497
- pendingGeometryUpdate = (corners: corners, gridDef: gridDef)
498
- return
499
- }
500
-
501
- DispatchQueue.global(qos: .userInitiated).async {
502
-
503
- var vertices: [Float] = []
504
- var indices: [UInt16] = []
505
-
506
- self.generateGeometryData(gridDef: gridDef, vertices: &vertices, indices: &indices)
507
-
508
- if vertices.isEmpty || indices.isEmpty {
509
- print("❌ [GridRenderLayer] No geometry generated")
510
- return
511
- }
512
-
513
- self.indexCount = indices.count
514
-
515
- DispatchQueue.main.async {
516
- self.vertexBuffer = self.device.makeBuffer(bytes: vertices, length: vertices.count * MemoryLayout<Float>.size, options: [])
517
- self.indexBuffer = self.device.makeBuffer(bytes: indices, length: indices.count * MemoryLayout<UInt16>.size, options: [])
518
-
519
- NotificationCenter.default.post(name: NSNotification.Name("TriggerMapRepaint"), object: nil)
520
- }
521
- }
522
- }
523
-
524
- private func decompressZstd(data: Data) -> Data? {
525
- let decompressedSize = data.withUnsafeBytes { ptr in
526
- ZSTD_getFrameContentSize(ptr.baseAddress, data.count)
527
- }
528
-
529
- guard Int64(bitPattern: decompressedSize) > 0 else {
530
- print("❌ [GridRenderLayer] Could not determine decompressed size")
531
- return nil
532
- }
533
-
534
- var decompressedData = Data(count: Int(decompressedSize))
535
-
536
- let result = decompressedData.withUnsafeMutableBytes { decompressedPtr -> Int in
537
- data.withUnsafeBytes { compressedPtr -> Int in
538
- let returnValue = ZSTD_decompress(
539
- decompressedPtr.baseAddress,
540
- Int(decompressedSize),
541
- compressedPtr.baseAddress,
542
- data.count
543
- )
544
- return Int(returnValue)
545
- }
546
- }
547
-
548
- guard result > 0 && result == decompressedData.count else {
549
- if result > 0 {
550
- let size_t_result = size_t(result)
551
- if let errorName = ZSTD_getErrorName(size_t_result) {
552
- let errorString = String(cString: errorName)
553
- print("❌ [GridRenderLayer] Zstd decompression failed: \(errorString)")
554
- }
555
- }
556
- return nil
557
- }
558
- return decompressedData
559
- }
560
-
561
- private func isLCCType(gridDef: [String: Any]) -> Bool {
562
- if let type = gridDef["type"] as? String {
563
- return type == "lambert_conformal_conic"
564
- }
565
- return false
566
- }
567
-
568
- private func generateLCCGeometry(gridDef: [String: Any], vertices: inout [Float], indices: inout [UInt16]) {
569
- guard let gridParams = gridDef["grid_params"] as? [String: Any],
570
- let projParams = gridDef["proj_params"] as? [String: Any],
571
- let nx = gridParams["nx"] as? Int,
572
- let ny = gridParams["ny"] as? Int,
573
- let dx = gridParams["dx"] as? Double,
574
- let dy = gridParams["dy"] as? Double,
575
- let x_origin = gridParams["x_origin"] as? Double,
576
- let y_origin = gridParams["y_origin"] as? Double else {
577
- return
578
- }
579
-
580
- let subdivisions = 60
581
- let TILE_SIZE: Double = 512.0
582
-
583
- let x_min = x_origin
584
- let y_max = y_origin
585
- let x_max = x_origin + Double(nx - 1) * dx
586
- let y_min = y_origin + Double(ny - 1) * dy
587
-
588
- var vertexGrid: [[VertexInfo?]] = Array(repeating: Array(repeating: nil, count: subdivisions + 1), count: subdivisions + 1)
589
- var validVertexCount: UInt16 = 0
590
-
591
- // Generate vertices
592
- for row in 0...subdivisions {
593
- for col in 0...subdivisions {
594
- let t_x = Double(col) / Double(subdivisions)
595
- let t_y = Double(row) / Double(subdivisions)
596
-
597
- let proj_x = x_min + t_x * (x_max - x_min)
598
- let proj_y = y_max + t_y * (y_min - y_max)
599
-
600
- // Convert LCC projection coordinates to lat/lon
601
- if let (lon, lat) = lccToLonLat(i: proj_x, j: proj_y, gridDef: gridDef) {
602
- // Convert lat/lon to Mercator
603
- let mercX_normalized = (lon + 180.0) / 360.0
604
- let clampedLat = max(-85.05112878, min(85.05112878, lat))
605
- let sinLatitude = sin(clampedLat * .pi / 180.0)
606
- let mercY_normalized = 0.5 - log((1 + sinLatitude) / (1 - sinLatitude)) / (4 * .pi)
607
-
608
- let mercX = mercX_normalized * TILE_SIZE
609
- let mercY = mercY_normalized * TILE_SIZE
610
-
611
- // Check for invalid values
612
- if !mercX.isFinite || !mercY.isFinite {
613
- vertexGrid[row][col] = nil
614
- continue
615
- }
616
-
617
- let tex_u = Float(t_x)
618
- let tex_v = Float(t_y)
619
-
620
- let vInfo = VertexInfo(
621
- mercX: Float(mercX),
622
- mercY: Float(mercY),
623
- texU: tex_u,
624
- texV: tex_v,
625
- index: validVertexCount
626
- )
627
-
628
- vertexGrid[row][col] = vInfo
629
-
630
- vertices.append(Float(mercX))
631
- vertices.append(Float(mercY))
632
- vertices.append(tex_u)
633
- vertices.append(tex_v)
634
-
635
- validVertexCount += 1
636
- } else {
637
- vertexGrid[row][col] = nil
638
- }
639
- }
640
- }
641
-
642
- if vertices.isEmpty {
643
- print("❌ [LCC Geometry] No valid vertices generated")
644
- return
645
- }
646
-
647
- // Generate indices
648
- for row in 0..<subdivisions {
649
- for col in 0..<subdivisions {
650
- guard let topLeft = vertexGrid[row][col],
651
- let topRight = vertexGrid[row][col + 1],
652
- let bottomLeft = vertexGrid[row + 1][col],
653
- let bottomRight = vertexGrid[row + 1][col + 1] else {
654
- continue
655
- }
656
-
657
- indices.append(topLeft.index)
658
- indices.append(bottomLeft.index)
659
- indices.append(topRight.index)
660
-
661
- indices.append(topRight.index)
662
- indices.append(bottomLeft.index)
663
- indices.append(bottomRight.index)
664
- }
665
- }
666
- }
667
-
668
- private func lccToLonLat(i: Double, j: Double, gridDef: [String: Any]) -> (lon: Double, lat: Double)? {
669
- guard let projParams = gridDef["proj_params"] as? [String: Any],
670
- let lat_0 = (projParams["lat_0"] as? NSNumber)?.doubleValue,
671
- let lon_0 = (projParams["lon_0"] as? NSNumber)?.doubleValue,
672
- let lat_1 = (projParams["lat_1"] as? NSNumber)?.doubleValue,
673
- let lat_2 = (projParams["lat_2"] as? NSNumber)?.doubleValue,
674
- let r_earth = (projParams["R"] as? NSNumber)?.doubleValue else {
675
- print("❌ [LCC Geometry] Failed to extract LCC parameters.")
676
- return nil
677
- }
678
-
679
- let π = Double.pi
680
- let toRad = π / 180.0
681
- let toDeg = 180.0 / π
682
-
683
- let lat1_rad = lat_1 * toRad
684
- let lat2_rad = lat_2 * toRad
685
- let lat0_rad = lat_0 * toRad
686
- let lon0_rad = lon_0 * toRad
687
-
688
- let n: Double
689
- if abs(lat_1 - lat_2) < 1e-10 {
690
- n = sin(lat1_rad)
691
- } else {
692
- n = log(cos(lat1_rad) / cos(lat2_rad)) / log(tan(π/4.0 + lat2_rad/2.0) / tan(π/4.0 + lat1_rad/2.0))
693
- }
694
-
695
- let F = cos(lat1_rad) * pow(tan(π/4.0 + lat1_rad/2.0), n) / n
696
- let rho_0 = r_earth * F * pow(tan(π/4.0 + lat0_rad/2.0), -n)
697
-
698
- let x = i
699
- let y = j
700
-
701
- let rho = sqrt(x * x + (rho_0 - y) * (rho_0 - y))
702
- if rho < 1e-10 {
703
- return (lon: lon_0, lat: lat_0)
704
- }
705
-
706
- let theta = atan2(x, rho_0 - y)
707
-
708
- let lon = lon0_rad + theta / n
709
- let lat = 2.0 * atan(pow(r_earth * F / rho, 1.0 / n)) - π/2.0
710
-
711
- let lonDeg = lon * toDeg
712
- let latDeg = lat * toDeg
713
-
714
- if !lonDeg.isFinite || !latDeg.isFinite {
715
- return nil
716
- }
717
-
718
- return (lon: lonDeg, lat: latDeg)
719
- }
720
-
721
- private func generateGeometryData(gridDef: [String: Any], vertices: inout [Float], indices: inout [UInt16]) {
722
- guard let gridParams = gridDef["grid_params"] as? [String: Any] else {
723
- print("❌ [generateGeometryData] No grid_params found")
724
- return
725
- }
726
-
727
- // Check grid type
728
- let isGFS = isGFSType(gridParams: gridParams)
729
- let isLCC = isLCCType(gridDef: gridDef)
730
-
731
- if isGFS {
732
- // GFS path remains unchanged
733
- let subdivisions = 120
734
- let verticesPerRow = (subdivisions * 3) + 1
735
- let TILE_SIZE: Double = 512.0
736
-
737
- for row in 0...subdivisions {
738
- for col in 0...(subdivisions * 3) {
739
- let v_interp = Float(row) / Float(subdivisions)
740
- let u_interp = Float(col) / Float(subdivisions)
741
- let lon = -540.0 + Double(u_interp) * 1080.0
742
- let lat = -90.0 + Double(v_interp) * 180.0
743
-
744
- let merc = lonLatToMercator(lon: lon, lat: lat, tileSize: TILE_SIZE)
745
- vertices.append(contentsOf: [merc.x, merc.y])
746
-
747
- let tex_u = Float((lon + 180.0) / 360.0)
748
- let tex_v = 1.0 - v_interp
749
- vertices.append(contentsOf: [tex_u, tex_v])
750
- }
751
- }
752
-
753
- for row in 0..<subdivisions {
754
- for col in 0..<(subdivisions * 3) {
755
- let tl = UInt16(row * verticesPerRow + col)
756
- let tr = tl + 1
757
- let bl = UInt16((row + 1) * verticesPerRow + col)
758
- let br = bl + 1
759
- indices.append(contentsOf: [tl, bl, tr, tr, bl, br])
760
- }
761
- }
762
- return
763
- }
764
-
765
- if isLCC {
766
- generateLCCGeometry(gridDef: gridDef, vertices: &vertices, indices: &indices)
767
- return
768
- }
769
-
770
- let nx = gridParams["nx"] as? Int ?? 0
771
- let ny = gridParams["ny"] as? Int ?? 0
772
- let lon_first = gridParams["lon_first"] as? Double ?? 0.0
773
- let lat_first = gridParams["lat_first"] as? Double ?? 90.0
774
- let dx = gridParams["dx_degrees"] as? Double ?? 0.0
775
- let dy = gridParams["dy_degrees"] as? Double ?? 0.0
776
- let lon_last = gridParams["lon_last"] as? Double ?? (lon_first + Double(nx - 1) * dx)
777
- let lat_last = gridParams["lat_last"] as? Double ?? (lat_first + Double(ny - 1) * dy)
778
-
779
- let lat_span = lat_last - lat_first
780
- let isSouthToNorth = lat_span > 0
781
-
782
- // Check if this is ECMWF and normalize longitudes
783
- let isECMWF = isECMWFType(gridParams: gridParams)
784
- var data_lon_first_180 = lon_first
785
- var data_lon_last_180 = lon_last
786
-
787
- if isECMWF {
788
- // Convert ECMWF's 180 to -180 range to -180 to 180
789
- data_lon_first_180 = lon_first >= 180 ? lon_first - 360 : lon_first
790
- data_lon_last_180 = lon_last >= 180 ? lon_last - 360 : lon_last
791
- } else {
792
- // For GFS and other models
793
- data_lon_first_180 = lon_first > 180 ? lon_first - 360 : lon_first
794
- data_lon_last_180 = lon_last > 180 ? lon_last - 360 : lon_last
795
- }
796
-
797
- let data_lon_range = data_lon_last_180 - data_lon_first_180
798
-
799
- let subdivisions_x = 120
800
- let subdivisions_y = 60
801
- let verticesPerRow = subdivisions_x + 1
802
- let TILE_SIZE: Double = 512.0
803
-
804
- let worldCopies = (data_lon_range > 300) ? [-1, 0, 1] : [0]
805
-
806
- for world_copy in worldCopies {
807
- let vertexStartIndex = UInt16(vertices.count / 4)
808
- let lon_offset = Double(world_copy) * 360.0
809
-
810
- for row in 0...subdivisions_y {
811
- for col in 0...subdivisions_x {
812
- let v_interp = Float(row) / Float(subdivisions_y)
813
- let u_interp = Float(col) / Float(subdivisions_x)
814
-
815
- let vertex_lon = data_lon_first_180 + (Double(u_interp) * data_lon_range)
816
- let vertex_lat = lat_first + (Double(v_interp) * lat_span)
817
-
818
- let mercX_normalized = ((vertex_lon + lon_offset) + 180.0) / 360.0
819
- let clampedLat = max(-85.05112878, min(85.05112878, vertex_lat))
820
- let sinLatitude = sin(clampedLat * .pi / 180.0)
821
- let mercY_normalized = 0.5 - log((1 + sinLatitude) / (1 - sinLatitude)) / (4 * .pi)
822
- let mercX = mercX_normalized * TILE_SIZE
823
- let mercY = mercY_normalized * TILE_SIZE
824
-
825
- vertices.append(contentsOf: [Float(mercX), Float(mercY)])
826
-
827
- let tex_u = u_interp
828
- let tex_v = isSouthToNorth ? (1.0 - v_interp) : v_interp
829
-
830
- vertices.append(contentsOf: [tex_u, tex_v])
831
- }
832
- }
833
-
834
- for row in 0..<UInt16(subdivisions_y) {
835
- for col in 0..<UInt16(subdivisions_x) {
836
- let tl = vertexStartIndex + row * UInt16(verticesPerRow) + col
837
- let tr = tl + 1
838
- let bl = vertexStartIndex + (row + 1) * UInt16(verticesPerRow) + col
839
- let br = bl + 1
840
-
841
- if isSouthToNorth {
842
- indices.append(contentsOf: [tl, bl, tr, tr, bl, br])
843
- } else {
844
- indices.append(contentsOf: [tl, tr, bl, bl, tr, br])
845
- }
846
- }
847
- }
848
- }
849
- }
850
-
851
- private func isGFSType(gridParams: [String: Any]) -> Bool {
852
- return (gridParams["lon_first"] as? Double) == 0.0 &&
853
- abs((gridParams["lat_first"] as? Double) ?? -1) == 90.0
854
- }
855
-
856
- private func isECMWFType(gridParams: [String: Any]) -> Bool {
857
- return (gridParams["lon_first"] as? Double) == 180.0 &&
858
- (gridParams["lat_first"] as? Double) == 90.0
859
- }
860
-
861
- private func lonLatToMercator(lon: Double, lat: Double, tileSize: Double) -> (x: Float, y: Float) {
862
- let mercX_normalized = (lon + 180.0) / 360.0
863
- let clampedLat = max(-85.05112878, min(85.05112878, lat))
864
- let sinLatitude = sin(clampedLat * .pi / 180.0)
865
- let mercY_normalized = 0.5 - log((1.0 + sinLatitude) / (1.0 - sinLatitude)) / (4.0 * .pi)
866
-
867
- return (x: Float(mercX_normalized * tileSize), y: Float(mercY_normalized * tileSize))
868
- }
869
-
870
- // MARK: - Internal methods for CustomLayerHost (called by wrapper)
871
-
872
- internal func internalRenderingWillStart(_ metalDevice: MTLDevice, colorPixelFormat: UInt, depthStencilPixelFormat: UInt) {
873
- self.device = metalDevice
874
- self.commandQueue = metalDevice.makeCommandQueue()
875
-
876
- let bundle = Bundle(for: GridRenderLayer.self)
877
-
878
- // Determine if we're running on simulator or device
879
- #if targetEnvironment(simulator)
880
- let metallibName = "Shaders-simulator"
881
- #else
882
- let metallibName = "Shaders-device"
883
- #endif
884
-
885
- // Try to load pre-compiled metallib for the current platform
886
- var defaultLibrary: MTLLibrary?
887
-
888
- // 1. Look in the main bundle level
889
- if let metallibUrl = bundle.url(forResource: metallibName, withExtension: "metallib") {
890
- print("ℹ️ [GridRenderLayer] Found pre-compiled shaders at: \(metallibUrl.lastPathComponent)")
891
- defaultLibrary = try? metalDevice.makeLibrary(URL: metallibUrl)
892
- }
893
-
894
- // 2. Fall back to compiled-shaders subdirectory
895
- if defaultLibrary == nil {
896
- if let metallibUrl = bundle.url(forResource: metallibName, withExtension: "metallib", subdirectory: "compiled-shaders") {
897
- print("ℹ️ [GridRenderLayer] Found pre-compiled shaders in subdirectory: \(metallibUrl.lastPathComponent)")
898
- defaultLibrary = try? metalDevice.makeLibrary(URL: metallibUrl)
899
- }
900
- }
901
-
902
- // 3. Fall back to compiling from .metal source (for development with npm link)
903
- if defaultLibrary == nil {
904
- if let metalUrl = bundle.url(forResource: "Shaders", withExtension: "metal") {
905
- print("⚠️ [GridRenderLayer] Pre-compiled shaders not found. Compiling from source: \(metalUrl.lastPathComponent)")
906
- if let source = try? String(contentsOf: metalUrl),
907
- let library = try? metalDevice.makeLibrary(source: source, options: nil) {
908
- defaultLibrary = library
909
- }
910
- }
911
- }
912
-
913
- // Neither worked
914
- if defaultLibrary == nil {
915
- print("❌ [GridRenderLayer] Could not find or compile Metal shaders")
916
- print(" Bundle path: \(bundle.bundlePath)")
917
- return
918
- }
919
-
920
- guard let library = defaultLibrary else {
921
- print("❌ [GridRenderLayer] Failed to create Metal library")
922
- return
923
- }
924
-
925
- let vertexFunction = library.makeFunction(name: "vertex_main")
926
- let fragmentFunction = library.makeFunction(name: "fragment_main")
927
-
928
- // Set up vertex descriptor
929
- let vertexDescriptor = MTLVertexDescriptor()
930
- vertexDescriptor.attributes[0].format = .float2 // position (x, y)
931
- vertexDescriptor.attributes[0].offset = 0
932
- vertexDescriptor.attributes[0].bufferIndex = 0
933
-
934
- vertexDescriptor.attributes[1].format = .float2 // texCoord (u, v)
935
- vertexDescriptor.attributes[1].offset = MemoryLayout<Float>.size * 2
936
- vertexDescriptor.attributes[1].bufferIndex = 0
937
-
938
- vertexDescriptor.layouts[0].stride = MemoryLayout<Float>.size * 4 // 2 floats for pos + 2 for texCoord
939
- vertexDescriptor.layouts[0].stepFunction = .perVertex
940
-
941
- let pipelineDescriptor = MTLRenderPipelineDescriptor()
942
- pipelineDescriptor.vertexDescriptor = vertexDescriptor
943
- pipelineDescriptor.vertexFunction = vertexFunction
944
- pipelineDescriptor.fragmentFunction = fragmentFunction
945
-
946
- pipelineDescriptor.colorAttachments[0].pixelFormat = MTLPixelFormat(rawValue: colorPixelFormat) ?? .bgra8Unorm
947
- pipelineDescriptor.depthAttachmentPixelFormat = MTLPixelFormat(rawValue: depthStencilPixelFormat) ?? .depth32Float_stencil8
948
- pipelineDescriptor.stencilAttachmentPixelFormat = MTLPixelFormat(rawValue: depthStencilPixelFormat) ?? .depth32Float_stencil8
949
-
950
- pipelineDescriptor.colorAttachments[0].isBlendingEnabled = true
951
- pipelineDescriptor.colorAttachments[0].rgbBlendOperation = .add
952
- pipelineDescriptor.colorAttachments[0].alphaBlendOperation = .add
953
- pipelineDescriptor.colorAttachments[0].sourceRGBBlendFactor = .sourceAlpha
954
- pipelineDescriptor.colorAttachments[0].sourceAlphaBlendFactor = .sourceAlpha
955
- pipelineDescriptor.colorAttachments[0].destinationRGBBlendFactor = .oneMinusSourceAlpha
956
- pipelineDescriptor.colorAttachments[0].destinationAlphaBlendFactor = .oneMinusSourceAlpha
957
-
958
- do {
959
- self.pipelineState = try metalDevice.makeRenderPipelineState(descriptor: pipelineDescriptor)
960
- } catch {
961
- print("❌ [GridRenderLayer] Failed to create pipeline state: \(error)")
962
- return
963
- }
964
-
965
- let dataSamplerDesc = MTLSamplerDescriptor()
966
- dataSamplerDesc.minFilter = .nearest
967
- dataSamplerDesc.magFilter = .nearest
968
- self.dataSamplerState = metalDevice.makeSamplerState(descriptor: dataSamplerDesc)
969
-
970
- let colormapSamplerDesc = MTLSamplerDescriptor()
971
- colormapSamplerDesc.minFilter = .nearest
972
- colormapSamplerDesc.magFilter = .nearest
973
- colormapSamplerDesc.sAddressMode = .clampToEdge
974
- self.colormapSamplerState = metalDevice.makeSamplerState(descriptor: colormapSamplerDesc)
975
-
976
- // Process any pending updates that came in before Metal was ready
977
- if let pendingGeometry = pendingGeometryUpdate {
978
- updateGeometry(corners: pendingGeometry.corners, gridDef: pendingGeometry.gridDef)
979
- pendingGeometryUpdate = nil
980
- }
981
-
982
- if let pendingColormap = pendingColormapUpdate {
983
- updateColormapTexture(colormapAsBase64: pendingColormap)
984
- pendingColormapUpdate = nil
985
- }
986
-
987
- if let pendingData = pendingDataUpdate {
988
- updateDataTexture(data: pendingData.data, nx: pendingData.nx, ny: pendingData.ny,
989
- scale: pendingData.scale, offset: pendingData.offset,
990
- missing: pendingData.missing, scaleType: pendingData.scaleType)
991
- pendingDataUpdate = nil
992
- }
993
- }
994
- internal func internalRender(_ parameters: CustomLayerRenderParameters, mtlCommandBuffer: MTLCommandBuffer, mtlRenderPassDescriptor: MTLRenderPassDescriptor) {
995
- guard isVisible,
996
- let pipeline = pipelineState,
997
- let vertices = vertexBuffer,
998
- let indices = indexBuffer,
999
- let dataTex = dataTexture,
1000
- let colormapTex = colormapTexture,
1001
- indexCount > 0,
1002
- let encoder = mtlCommandBuffer.makeRenderCommandEncoder(descriptor: mtlRenderPassDescriptor) else {
1003
-
1004
- return
1005
- }
1006
-
1007
- let needsLinear = (uniforms.smoothing != 0)
1008
- if isDataSamplerLinear != needsLinear {
1009
- let samplerDesc = MTLSamplerDescriptor()
1010
- samplerDesc.minFilter = needsLinear ? .linear : .nearest
1011
- samplerDesc.magFilter = needsLinear ? .linear : .nearest
1012
- dataSamplerState = device.makeSamplerState(descriptor: samplerDesc)
1013
- isDataSamplerLinear = needsLinear
1014
- }
1015
-
1016
- // Apply zoom scale transformation
1017
- let zoom = parameters.zoom
1018
- let scale = Float(pow(2.0, zoom))
1019
-
1020
- let matrixArray = parameters.projectionMatrix
1021
- var floatArray = matrixArray.map { $0.floatValue }
1022
-
1023
- floatArray[0] *= scale // X scale
1024
- floatArray[1] *= scale // X rotation
1025
- floatArray[2] *= scale // X Z-component
1026
- floatArray[3] *= scale // X translation
1027
- floatArray[4] *= scale // Y rotation
1028
- floatArray[5] *= scale // Y scale
1029
- floatArray[6] *= scale // Y Z-component
1030
- floatArray[7] *= scale // Y translation
1031
- floatArray[8] *= scale // Z X-component
1032
- floatArray[9] *= scale // Z Y-component
1033
- floatArray[10] *= scale // Z scale
1034
- floatArray[11] *= scale // Z translation
1035
-
1036
- let mvp = matrix_float4x4(
1037
- SIMD4<Float>(floatArray[0], floatArray[1], floatArray[2], floatArray[3]),
1038
- SIMD4<Float>(floatArray[4], floatArray[5], floatArray[6], floatArray[7]),
1039
- SIMD4<Float>(floatArray[8], floatArray[9], floatArray[10], floatArray[11]),
1040
- SIMD4<Float>(floatArray[12], floatArray[13], floatArray[14], floatArray[15])
1041
- )
1042
-
1043
- // Add depth state
1044
- let depthStencilDescriptor = MTLDepthStencilDescriptor()
1045
- depthStencilDescriptor.depthCompareFunction = .always
1046
- depthStencilDescriptor.isDepthWriteEnabled = false
1047
- let depthStencilState = device.makeDepthStencilState(descriptor: depthStencilDescriptor)
1048
-
1049
- encoder.setRenderPipelineState(pipeline)
1050
- encoder.setDepthStencilState(depthStencilState!)
1051
- encoder.setVertexBuffer(vertices, offset: 0, index: 0)
1052
- encoder.setVertexBytes([mvp], length: MemoryLayout<matrix_float4x4>.size, index: 1)
1053
-
1054
- var mutableUniforms = uniforms
1055
- encoder.setFragmentBytes(&mutableUniforms, length: MemoryLayout<FragmentUniforms>.stride, index: 0)
1056
- encoder.setFragmentTexture(dataTex, index: 0)
1057
- encoder.setFragmentTexture(colormapTex, index: 1)
1058
- encoder.setFragmentSamplerState(dataSamplerState, index: 0)
1059
- encoder.setFragmentSamplerState(colormapSamplerState, index: 1)
1060
-
1061
- encoder.drawIndexedPrimitives(type: .triangle, indexCount: indexCount, indexType: .uint16, indexBuffer: indices, indexBufferOffset: 0)
1062
-
1063
- encoder.endEncoding()
1064
- }
1065
-
1066
- deinit {
1067
- print("🔴 [GridRenderLayer] deinit called for layer: \(id)")
1068
-
1069
- // Clean up all resources
1070
- frameCacheQueue.sync {
1071
- frameCache.removeAll()
1072
- }
1073
- vertexBuffer = nil
1074
- indexBuffer = nil
1075
- dataTexture = nil
1076
- colormapTexture = nil
1077
- device = nil
1078
- commandQueue = nil
1079
- pipelineState = nil
1080
- dataSamplerState = nil
1081
- colormapSamplerState = nil
1082
-
1083
- // Break the reference cycle
1084
- hostWrapper = nil
1085
- }
1086
-
1087
- internal func internalRenderingWillEnd() {
1088
- // DO NOT clear these here, otherwise the layer disappears during map transitions/resizing
1089
- // vertexBuffer = nil
1090
- // indexBuffer = nil
1091
- // dataTexture = nil
1092
- // colormapTexture = nil
1093
- }
1094
- }
1095
-
1096
- extension GridRenderLayer {
1097
- // Override to prevent KVC crashes during cleanup
1098
- open override func value(forUndefinedKey key: String) -> Any? {
1099
- print("⚠️ [GridRenderLayer] Attempted to access undefined key: \(key)")
1100
- return nil
1101
- }
1102
-
1103
- open override func setValue(_ value: Any?, forUndefinedKey key: String) {
1104
- print("⚠️ [GridRenderLayer] Attempted to set undefined key: \(key)")
1105
- // Silently ignore instead of crashing
1106
- }
1107
- }