Skip to content

Commit

Permalink
fix macos builds
Browse files Browse the repository at this point in the history
  • Loading branch information
yousifd committed Feb 11, 2025
1 parent 6fcc13b commit 4a12cbf
Show file tree
Hide file tree
Showing 35 changed files with 99 additions and 73 deletions.
5 changes: 3 additions & 2 deletions platform/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ load("//bazel:flags.bzl", "CPP_FLAGS", "MAPLIBRE_FLAGS", "WARNING_FLAGS")
objc_library(
name = "macos-objcpp",
srcs = [
"//platform/darwin:darwin_gltf_hdrs",
"//platform/darwin:darwin_gltf_srcs",
"//platform/darwin:darwin_objcpp_custom_drawable_srcs",
"//platform/darwin:darwin_objcpp_srcs",
"//platform/darwin:darwin_private_hdrs",
Expand Down Expand Up @@ -36,6 +38,7 @@ objc_library(
"CoreLocation",
"AppKit",
"SystemConfiguration",
"Accelerate",
],
visibility = ["//visibility:public"],
deps = [
Expand All @@ -50,8 +53,6 @@ objc_library(
objc_library(
name = "macos-objc",
srcs = [
"//platform/darwin:darwin_gltf_hdrs",
"//platform/darwin:darwin_gltf_srcs",
"//platform/darwin:darwin_objc_hdrs",
"//platform/darwin:darwin_objc_srcs",
"//platform/darwin:darwin_private_hdrs",
Expand Down
54 changes: 26 additions & 28 deletions platform/darwin/bazel/files.bzl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
MLN_GLTF_HEADERS = [
"src/gltf/GLTFModelLayer.h"
"src/gltf/GLTFModelLayer.h",
]

MLN_GLTF_SOURCE = [
Expand All @@ -20,68 +20,66 @@ MLN_GLTF_SOURCE = [
"src/gltf/GLTFModelLayer.h",
"src/gltf/GLTFModelLayer.mm",
"src/gltf/GLTFModelLoader.h",
"src/gltf/GLTFModelLoader.m",
"src/gltf/GLTFModelLoader.mm",
"src/gltf/GLTFMTLRenderItem.h",
"src/gltf/GLTFMTLRenderItem.m",
"src/gltf/GLTFMTLRenderItem.mm",
"src/gltf/GLTFRenderer.hpp",
"src/gltf/GLTFRenderer.cpp",
"src/gltf/MetalRenderer+GLTFAsset.mm",
"src/gltf/gltfkit/GLTFAsset.h",
"src/gltf/gltfkit/GLTFAsset.m",
"src/gltf/gltfkit/GLTFAsset.mm",
"src/gltf/gltfkit/GLTFAccessor.h",
"src/gltf/gltfkit/GLTFAccessor.m",
"src/gltf/gltfkit/GLTFAccessor.mm",
"src/gltf/gltfkit/GLTFAnimation.h",
"src/gltf/gltfkit/GLTFAnimation.m",
"src/gltf/gltfkit/GLTFAnimation.mm",
"src/gltf/gltfkit/GLTFBinaryChunk.h",
"src/gltf/gltfkit/GLTFBinaryChunk.m",
"src/gltf/gltfkit/GLTFBinaryChunk.mm",
"src/gltf/gltfkit/GLTFBuffer.h",
"src/gltf/gltfkit/GLTFBufferAllocator.h",
"src/gltf/gltfkit/GLTFBufferView.h",
"src/gltf/gltfkit/GLTFBufferView.m",
"src/gltf/gltfkit/GLTFBufferView.mm",
"src/gltf/gltfkit/GLTFCamera.h",
"src/gltf/gltfkit/GLTFCamera.m",
"src/gltf/gltfkit/GLTFCamera.mm",
"src/gltf/gltfkit/GLTFDefaultBufferAllocator.h",
"src/gltf/gltfkit/GLTFDefaultBufferAllocator.m",
"src/gltf/gltfkit/GLTFDefaultBufferAllocator.mm",
"src/gltf/gltfkit/GLTFEnums.h",
"src/gltf/gltfkit/GLTFExtensionNames.h",
"src/gltf/gltfkit/GLTFExtensionNames.m",
"src/gltf/gltfkit/GLTFExtensionNames.mm",
"src/gltf/gltfkit/GLTFImage.h",
"src/gltf/gltfkit/GLTFImage.m",
"src/gltf/gltfkit/GLTFImage.mm",
"src/gltf/gltfkit/GLTFKHRLight.h",
"src/gltf/gltfkit/GLTFKHRLight.m",
"src/gltf/gltfkit/GLTFKHRLight.mm",
"src/gltf/gltfkit/GLTFMaterial.h",
"src/gltf/gltfkit/GLTFMaterial.m",
"src/gltf/gltfkit/GLTFMaterial.mm",
"src/gltf/gltfkit/GLTFMesh.h",
"src/gltf/gltfkit/GLTFMesh.m",
"src/gltf/gltfkit/GLTFMesh.mm",
"src/gltf/gltfkit/GLTFMTLBufferAllocator.h",
"src/gltf/gltfkit/GLTFMTLBufferAllocator.m",
"src/gltf/gltfkit/GLTFMTLBufferAllocator.mm",
"src/gltf/gltfkit/GLTFMTLShaderBuilder.h",
"src/gltf/gltfkit/GLTFMTLShaderBuilder.m",
"src/gltf/gltfkit/GLTFMTLShaderBuilder.mm",
"src/gltf/gltfkit/GLTFMTLTextureLoader.h",
"src/gltf/gltfkit/GLTFMTLTextureLoader.m",
"src/gltf/gltfkit/GLTFMTLTextureLoader.mm",
"src/gltf/gltfkit/GLTFMTLUtilities.h",
"src/gltf/gltfkit/GLTFMTLUtilities.m",
"src/gltf/gltfkit/GLTFMTLUtilities.mm",
"src/gltf/gltfkit/GLTFNode.h",
"src/gltf/gltfkit/GLTFNode.m",
"src/gltf/gltfkit/GLTFNode.mm",
"src/gltf/gltfkit/GLTFNodeVisitor.h",
"src/gltf/gltfkit/GLTFObject.h",
"src/gltf/gltfkit/GLTFObject.m",
"src/gltf/gltfkit/GLTFObject.mm",
"src/gltf/gltfkit/GLTFScene.h",
"src/gltf/gltfkit/GLTFScene.m",
"src/gltf/gltfkit/GLTFScene.mm",
"src/gltf/gltfkit/GLTFSkin.h",
"src/gltf/gltfkit/GLTFSkin.m",
"src/gltf/gltfkit/GLTFSkin.mm",
"src/gltf/gltfkit/GLTFTexture.h",
"src/gltf/gltfkit/GLTFTexture.mm",
"src/gltf/gltfkit/GLTFTextureSampler.h",
"src/gltf/gltfkit/GLTFTextureSampler.m",
"src/gltf/gltfkit/GLTFTextureSampler.mm",
"src/gltf/gltfkit/GLTFUtilities.h",
"src/gltf/gltfkit/GLTFUtilities.m",
"src/gltf/gltfkit/GLTFUtilities.mm",
"src/gltf/gltfkit/GLTFVertexDescriptor.h",
"src/gltf/gltfkit/GLTFVertexDescriptor.m",

"src/gltf/gltfkit/GLTFVertexDescriptor.mm",
]


MLN_GENERATED_DARWIN_STYLE_SOURCE = [
"src/MLNLight.mm",
"src/MLNBackgroundStyleLayer.mm",
Expand Down
6 changes: 0 additions & 6 deletions platform/darwin/src/gltf/GLTFModelLayer.mm
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#import <Metal/Metal.h>
#import "MLNBackendResource.h"
#import "MLNMapView.h"
#import "MLNMapProjection.h"
#include <memory>
#import "GLTFManagerRenderingEnvironmentMetal.hpp"
#import "GLTFManager.hpp"
Expand Down Expand Up @@ -344,11 +343,6 @@ - (void)drawInMapView:(MLNMapView *)mapView withContext:(MLNStyleLayerDrawingCon
_viewportSize.y = resource.mtkView.drawableSize.height;
_manager->setDrawableSize(_viewportSize.x, _viewportSize.y);

MLNMapProjection *proj2 = mapView.mapProjection;
_manager->_metersPerPixel = proj2.metersPerPoint / 2.0;
_manager->setTiltDeg(90-mapView.camera.pitch);
_manager->setRotationDeg(-mapView.camera.heading);

float timestep = (1 / 60.0f);
_manager->updateScene(timestep);

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
//

#import "GLTFAnimation.h"
#include "GLTFUtilities.h"
#import "GLTFAccessor.h"
#import "GLTFBufferView.h"
#import "GLTFBuffer.h"
Expand All @@ -27,11 +28,11 @@ - (NSString *)description {
}

- (const void *)inputValues {
return [self.inputAccessor.bufferView.buffer contents] + self.inputAccessor.bufferView.offset + self.inputAccessor.offset;
return (uint8_t*)[self.inputAccessor.bufferView.buffer contents] + self.inputAccessor.bufferView.offset + self.inputAccessor.offset;
}

- (const void *)outputValues {
return [self.outputAccessor.bufferView.buffer contents] + self.outputAccessor.bufferView.offset + self.outputAccessor.offset;
return (uint8_t*)[self.outputAccessor.bufferView.buffer contents] + self.outputAccessor.bufferView.offset + self.outputAccessor.offset;
}

- (int)keyFrameCount {
Expand All @@ -48,14 +49,14 @@ - (NSString *)description {

- (NSTimeInterval)startTime {
GLTFAnimationSampler *sampler = self.sampler;
const float *timeValues = sampler.inputValues;
const float *timeValues = (const float*)sampler.inputValues;
float startTime = timeValues[0];
return startTime;
}

- (NSTimeInterval)endTime {
GLTFAnimationSampler *sampler = self.sampler;
const float *timeValues = sampler.inputValues;
const float *timeValues = (const float*)sampler.inputValues;
int keyFrameCount = sampler.keyFrameCount;
float endTime = timeValues[keyFrameCount - 1];
return endTime;
Expand All @@ -81,7 +82,7 @@ - (void)runAtTime:(NSTimeInterval)time {

int keyFrameCount = sampler.keyFrameCount;

const float *timeValues = sampler.inputValues;
const float *timeValues = (const float*)sampler.inputValues;

float minTime = timeValues[0];
float maxTime = timeValues[keyFrameCount - 1];
Expand Down Expand Up @@ -115,19 +116,19 @@ - (void)runAtTime:(NSTimeInterval)time {
NSLog(@"WARNING: Only float accessors are supported for rotation animations. This will only be reported once.");
});
}
const GLTFQuaternion *rotationValues = sampler.outputValues;
const GLTFQuaternion *rotationValues = (const GLTFQuaternion*)sampler.outputValues;

GLTFQuaternion previousRotation = rotationValues[previousKeyFrame];
GLTFQuaternion nextRotation = rotationValues[nextKeyFrame];
GLTFQuaternion interpRotation = simd_slerp(previousRotation, nextRotation, frameProgress);

target.rotationQuaternion = interpRotation;
} else if ([path isEqualToString:@"translation"]) {
const GLTFVector3 *translationValues = sampler.outputValues;
const GLTFVector3 *translationValues = (const GLTFVector3*)sampler.outputValues;

GLTFVector3 previousTranslation = translationValues[previousKeyFrame];
GLTFVector3 nextTranslation = translationValues[nextKeyFrame];

GLTFVector3 interpTranslation = (GLTFVector3) {
((1 - frameProgress) * previousTranslation.x) + (frameProgress * nextTranslation.x),
((1 - frameProgress) * previousTranslation.y) + (frameProgress * nextTranslation.y),
Expand All @@ -136,7 +137,7 @@ - (void)runAtTime:(NSTimeInterval)time {

target.translation = (simd_float3){ interpTranslation.x, interpTranslation.y, interpTranslation.z };
} else if ([path isEqualToString:@"scale"]) {
const float *scaleValues = sampler.outputValues;
const float *scaleValues = (const float*)sampler.outputValues;

float previousScale = scaleValues[previousKeyFrame];
float nextScale = scaleValues[nextKeyFrame];
Expand All @@ -151,7 +152,7 @@ - (void)runAtTime:(NSTimeInterval)time {
NSLog(@"WARNING: Only scalar float accessors are supported for weight animations. This will only be reported once.");
});
}
const float *weightValues = sampler.outputValues;
const float *weightValues = (const float*)sampler.outputValues;

long weightCount = sampler.outputAccessor.count / keyFrameCount;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
//

#import "GLTFAsset.h"
#include "GLTFEnums.h"
#import "GLTFAnimation.h"
#import "GLTFAccessor.h"
#import "GLTFBinaryChunk.h"
Expand Down Expand Up @@ -263,11 +264,11 @@ - (void)readBinaryChunks:(NSData *)assetData {

[assetData getBytes:&chunkHeader range:NSMakeRange(offset, sizeof(chunkHeader))];

NSData *chunkData = [NSData dataWithBytesNoCopy:(void *)(assetData.bytes + offset + sizeof(chunkHeader))
NSData *chunkData = [NSData dataWithBytesNoCopy:(void *)((uint8_t*)assetData.bytes + offset + sizeof(chunkHeader))
length:chunkHeader.length
freeWhenDone:NO];
chunk.data = chunkData;
chunk.chunkType = chunkHeader.type;
chunk.chunkType = (GLTFChunkType)chunkHeader.type;

[chunks addObject:chunk];

Expand Down Expand Up @@ -296,7 +297,7 @@ - (BOOL)loadAccessors:(NSArray *)accessorsMap {
NSMutableArray *accessors = [NSMutableArray arrayWithCapacity:accessorsMap.count];
for (NSDictionary *properties in accessorsMap) {
GLTFAccessor *accessor = [[GLTFAccessor alloc] init];
accessor.componentType = [properties[@"componentType"] integerValue];
accessor.componentType = (GLTFDataType)[properties[@"componentType"] integerValue];
accessor.dimension = GLTFDataDimensionForName(properties[@"type"]);
accessor.offset = [properties[@"byteOffset"] integerValue];
accessor.count = [properties[@"count"] integerValue];
Expand All @@ -315,7 +316,7 @@ - (BOOL)loadAccessors:(NSArray *)accessorsMap {
NSLog(@"WARNING: Accessor had misaligned offset %d, which is not a multiple of %d. Building auxiliary buffer of length %d and continuing...",
(int)dataOffset, (int)alignment, (int)length);
id<GLTFBuffer> buffer = [_bufferAllocator newBufferWithLength:length];
memcpy(buffer.contents, accessor.bufferView.buffer.contents + accessor.bufferView.offset + accessor.offset, buffer.length);
memcpy(buffer.contents, (uint8_t*)accessor.bufferView.buffer.contents + accessor.bufferView.offset + accessor.offset, buffer.length);
_buffers = [_buffers arrayByAddingObject:buffer];

GLTFBufferView *bufferView = [GLTFBufferView new];
Expand All @@ -329,7 +330,7 @@ - (BOOL)loadAccessors:(NSArray *)accessorsMap {
}
}

__block GLTFValueRange valueRange = { 0 };
__block GLTFValueRange valueRange = { {}, {} };
NSArray *minValues = properties[@"min"];
[minValues enumerateObjectsUsingBlock:^(NSNumber *num, NSUInteger index, BOOL *stop) {
valueRange.minValue[index] = num.floatValue;
Expand Down Expand Up @@ -408,7 +409,7 @@ - (BOOL)loadBufferViews:(NSArray *)bufferViewsMap {
bufferView.length = [properties[@"byteLength"] integerValue];
bufferView.stride = [properties[@"byteStride"] integerValue];
bufferView.offset = [properties[@"byteOffset"] integerValue];
bufferView.target = [properties[@"target"] integerValue];
bufferView.target = (GLTFTarget)[properties[@"target"] integerValue];

// if ((bufferView.buffer != nil) && (bufferView.offset % 16 != 0)) {
// NSLog(@"WARNING: Buffer view %d had misaligned offset of %d. Creating auxilliary buffer of length %d and continuing...",
Expand All @@ -435,10 +436,10 @@ - (BOOL)loadSamplers:(NSArray *)samplersMap {
NSMutableArray *samplers = [NSMutableArray arrayWithCapacity:samplersMap.count];
for (NSDictionary *properties in samplersMap) {
GLTFTextureSampler *sampler = [[GLTFTextureSampler alloc] init];
sampler.minFilter = [properties[@"minFilter"] integerValue] ?: sampler.minFilter;
sampler.magFilter = [properties[@"magFilter"] integerValue] ?: sampler.magFilter;
sampler.sAddressMode = [properties[@"wrapS"] integerValue] ?: sampler.sAddressMode;
sampler.tAddressMode = [properties[@"wrapT"] integerValue] ?: sampler.tAddressMode;
sampler.minFilter = (GLTFSamplingFilter)([properties[@"minFilter"] integerValue] ?: sampler.minFilter);
sampler.magFilter = (GLTFSamplingFilter)([properties[@"magFilter"] integerValue] ?: sampler.magFilter);
sampler.sAddressMode = (GLTFAddressMode)([properties[@"wrapS"] integerValue] ?: sampler.sAddressMode);
sampler.tAddressMode = (GLTFAddressMode)([properties[@"wrapT"] integerValue] ?: sampler.tAddressMode);
sampler.name = properties[@"name"];
sampler.extensions = properties[@"extensions"];
sampler.extras = properties[@"extras"];
Expand Down Expand Up @@ -510,10 +511,10 @@ - (BOOL)loadTextures:(NSArray *)texturesMap {
texture.image = _images[imageIndex];
}

texture.format = [properties[@"format"] integerValue] ?: texture.format;
texture.internalFormat = [properties[@"internalFormat"] integerValue] ?: texture.internalFormat;
texture.target = [properties[@"target"] integerValue] ?: texture.target;
texture.type = [properties[@"type"] integerValue] ?: texture.type;
texture.format = (GLTFTextureFormat)([properties[@"format"] integerValue] ?: texture.format);
texture.internalFormat = (GLTFTextureFormat)([properties[@"internalFormat"] integerValue] ?: texture.internalFormat);
texture.target = (GLTFTextureTarget)([properties[@"target"] integerValue] ?: texture.target);
texture.type = (GLTFTextureType)([properties[@"type"] integerValue] ?: texture.type);
texture.name = properties[@"name"];
texture.extensions = properties[@"extensions"];
texture.extras = properties[@"extras"];
Expand Down Expand Up @@ -665,12 +666,12 @@ - (BOOL)loadMeshes:(NSArray *)meshesMap {
NSUInteger indexAccessorIndex = [submeshProperties[@"indices"] intValue];
if (indexAccessorIndex < _accessors.count) {
GLTFAccessor *indexAccessor = _accessors[indexAccessorIndex];
if (indexAccessor.componentType == GLTFTextureTypeUChar) {
if (indexAccessor.componentType == (GLTFDataType)GLTFTextureTypeUChar) {
// Fix up 8-bit indices, since they're unsupported in modern APIs
uint8_t *sourceIndices = indexAccessor.bufferView.buffer.contents + indexAccessor.offset + indexAccessor.bufferView.offset;
uint8_t *sourceIndices = (uint8_t*)indexAccessor.bufferView.buffer.contents + indexAccessor.offset + indexAccessor.bufferView.offset;

id<GLTFBuffer> shortBuffer = [_bufferAllocator newBufferWithLength:indexAccessor.count * sizeof(uint16_t)];
uint16_t *destIndices = shortBuffer.contents;
uint16_t *destIndices = (uint16_t*)shortBuffer.contents;
for (int i = 0; i < indexAccessor.count; ++i) {
destIndices[i] = (uint16_t)sourceIndices[i];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
vImageConverter_Release(converter);
CFRelease(srcData);

return dstPixels;
return (__fp16*)dstPixels;
}

unsigned char *GLTFMTLConvertImageToRGBA8U(CGImageRef image)
Expand Down Expand Up @@ -108,7 +108,7 @@
.bitsPerComponent = sizeof(unsigned char) * 8,
.bitsPerPixel = sizeof(unsigned char) * 8 * 4,
.colorSpace = dstColorSpace,
.bitmapInfo = kCGBitmapByteOrder32Big | kCGImageAlphaLast
.bitmapInfo = (CGBitmapInfo)kCGBitmapByteOrder32Big | (CGBitmapInfo)kCGImageAlphaLast
};

vImage_Error error = kvImageNoError;
Expand Down Expand Up @@ -138,7 +138,7 @@
vImageConverter_Release(converter);
CFRelease(srcData);

return dstPixels;
return (unsigned char*)dstPixels;
}

@interface GLTFMTLTextureLoader ()
Expand Down Expand Up @@ -201,7 +201,7 @@ - (instancetype)initWithDevice:(id<MTLDevice>)device {
height:height
mipmapped:mipmapped];

id<MTLTexture> texture = [self newTextureWithBytes:dstBytes
id<MTLTexture> texture = [self newTextureWithBytes:(const unsigned char*)dstBytes
bytesPerRow:bytesPerRow
descriptor:descriptor
options:options
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@ MTLPrimitiveType GLTFMTLPrimitiveTypeForPrimitiveType(GLTFPrimitiveType gltfType
case GLTFPrimitiveTypeTriangleStrip:
return MTLPrimitiveTypeTriangleStrip;

// Not supported; need to duplicate first element and restitch into tri strip, respectively
// Not supported; need to duplicate first element and restitch into tri strip, respectively
case GLTFPrimitiveTypeLineLoop:
case GLTFPrimitiveTypeTriangleFan:
default:
return -1;
NSLog(@"Unsupported primitive type function %d", (int)gltfType);
return MTLPrimitiveTypePoint;
}
}

Expand Down
File renamed without changes.
Loading

0 comments on commit 4a12cbf

Please sign in to comment.