Skip to content

Commit

Permalink
Fix macOS warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
RobDangerous committed Oct 31, 2024
1 parent 2154ec4 commit 2f16a4a
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ kinc_g4_constant_location_t kinc_g4_compute_shader_get_constant_location(kinc_g4

kinc_g4_texture_unit_t kinc_g4_compute_shader_get_texture_unit(kinc_g4_compute_shader *shader, const char *name) {
kinc_g5_texture_unit_t g5_unit = kinc_g5_compute_shader_get_texture_unit(&shader->impl.shader, name);
kinc_g4_texture_unit_t g4_unit;
kinc_g4_texture_unit_t g4_unit = {0};
for (int i = 0; i < KINC_G4_SHADER_TYPE_COUNT; ++i) {
g4_unit.stages[i] = g5_unit.stages[i];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ kinc_g5_constant_location_t kinc_g5_compute_shader_get_constant_location(kinc_g5
}

kinc_g5_texture_unit_t kinc_g5_compute_shader_get_texture_unit(kinc_g5_compute_shader *shader, const char *name) {
kinc_g5_texture_unit_t unit;
kinc_g5_texture_unit_t unit = {0};
for (int i = 0; i < KINC_G5_SHADER_TYPE_COUNT; ++i) {
unit.stages[i] = -1;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ id getMetalQueue(void);

id<MTLComputePipelineState> _raytracing_pipeline;
NSMutableArray *_primitive_accels;
API_AVAILABLE(macos(11.0))
id<MTLAccelerationStructure> _instance_accel;
dispatch_semaphore_t _sem;

Expand All @@ -41,61 +42,63 @@ void kinc_raytrace_pipeline_init(kinc_raytrace_pipeline_t *pipeline, kinc_g5_com

void kinc_raytrace_pipeline_destroy(kinc_raytrace_pipeline_t *pipeline) {}

API_AVAILABLE(macos(11.0))
id<MTLAccelerationStructure> create_acceleration_sctructure(MTLAccelerationStructureDescriptor *descriptor) {
id<MTLDevice> device = getMetalDevice();
id<MTLCommandQueue> queue = getMetalQueue();

MTLAccelerationStructureSizes accel_sizes = [device accelerationStructureSizesWithDescriptor:descriptor];
id<MTLAccelerationStructure> acceleration_structure = [device newAccelerationStructureWithSize:accel_sizes.accelerationStructureSize];

id<MTLBuffer> scratch_buffer = [device newBufferWithLength:accel_sizes.buildScratchBufferSize options:MTLResourceStorageModePrivate];
id<MTLCommandBuffer> command_buffer = [queue commandBuffer];
id<MTLAccelerationStructureCommandEncoder> command_encoder = [command_buffer accelerationStructureCommandEncoder];
id<MTLBuffer> compacteds_size_buffer = [device newBufferWithLength:sizeof(uint32_t) options:MTLResourceStorageModeShared];

[command_encoder buildAccelerationStructure:acceleration_structure descriptor:descriptor scratchBuffer:scratch_buffer scratchBufferOffset:0];

[command_encoder writeCompactedAccelerationStructureSize:acceleration_structure toBuffer:compacteds_size_buffer offset:0];

[command_encoder endEncoding];
[command_buffer commit];
[command_buffer waitUntilCompleted];

uint32_t compacted_size = *(uint32_t *)compacteds_size_buffer.contents;
id<MTLAccelerationStructure> compacted_acceleration_structure = [device newAccelerationStructureWithSize:compacted_size];
command_buffer = [queue commandBuffer];
command_encoder = [command_buffer accelerationStructureCommandEncoder];
[command_encoder copyAndCompactAccelerationStructure:acceleration_structure toAccelerationStructure:compacted_acceleration_structure];
[command_encoder endEncoding];
[command_buffer commit];

return compacted_acceleration_structure;
}

API_AVAILABLE(macos(11.0))
void kinc_raytrace_acceleration_structure_init(kinc_raytrace_acceleration_structure_t *accel, kinc_g5_command_list_t *command_list, kinc_g5_vertex_buffer_t *vb,
kinc_g5_index_buffer_t *ib) {
kinc_g5_index_buffer_t *ib) {
#if !TARGET_OS_IPHONE
MTLResourceOptions options = MTLResourceStorageModeManaged;
#else
MTLResourceOptions options = MTLResourceStorageModeShared;
#endif

MTLAccelerationStructureTriangleGeometryDescriptor *descriptor = [MTLAccelerationStructureTriangleGeometryDescriptor descriptor];
descriptor.indexType = MTLIndexTypeUInt32;
descriptor.indexBuffer = (__bridge id<MTLBuffer>)ib->impl.metal_buffer;
descriptor.vertexBuffer = (__bridge id<MTLBuffer>)vb->impl.mtlBuffer;
descriptor.vertexStride = vb->impl.myStride;
descriptor.triangleCount = ib->impl.count / 3;

MTLPrimitiveAccelerationStructureDescriptor *accel_descriptor = [MTLPrimitiveAccelerationStructureDescriptor descriptor];
accel_descriptor.geometryDescriptors = @[ descriptor ];
id<MTLAccelerationStructure> acceleration_structure = create_acceleration_sctructure(accel_descriptor);
_primitive_accels = [[NSMutableArray alloc] init];
[_primitive_accels addObject:acceleration_structure];

id<MTLDevice> device = getMetalDevice();
id<MTLBuffer> instance_buffer = [device newBufferWithLength:sizeof(MTLAccelerationStructureInstanceDescriptor) * 1 options:options];

MTLAccelerationStructureInstanceDescriptor *instance_descriptors = (MTLAccelerationStructureInstanceDescriptor *)instance_buffer.contents;
instance_descriptors[0].accelerationStructureIndex = 0;
instance_descriptors[0].options = MTLAccelerationStructureInstanceOptionOpaque;
Expand All @@ -104,11 +107,11 @@ void kinc_raytrace_acceleration_structure_init(kinc_raytrace_acceleration_struct
instance_descriptors[0].transformationMatrix.columns[1] = MTLPackedFloat3Make(0, 1, 0);
instance_descriptors[0].transformationMatrix.columns[2] = MTLPackedFloat3Make(0, 0, 1);
instance_descriptors[0].transformationMatrix.columns[3] = MTLPackedFloat3Make(0, 0, 0);

#if !TARGET_OS_IPHONE
[instance_buffer didModifyRange:NSMakeRange(0, instance_buffer.length)];
#endif

MTLInstanceAccelerationStructureDescriptor *inst_accel_descriptor = [MTLInstanceAccelerationStructureDescriptor descriptor];
inst_accel_descriptor.instancedAccelerationStructures = _primitive_accels;
inst_accel_descriptor.instanceCount = 1;
Expand All @@ -130,36 +133,39 @@ void kinc_raytrace_set_target(kinc_g5_texture_t *_output) {
output = _output;
}

API_AVAILABLE(macos(11.0))
void kinc_raytrace_dispatch_rays(kinc_g5_command_list_t *command_list) {
dispatch_semaphore_wait(_sem, DISPATCH_TIME_FOREVER);

id<MTLCommandQueue> queue = getMetalQueue();
id<MTLCommandBuffer> command_buffer = [queue commandBuffer];
__block dispatch_semaphore_t sem = _sem;
[command_buffer addCompletedHandler:^(id<MTLCommandBuffer> buffer) {
dispatch_semaphore_signal(sem);
}];

NSUInteger width = output->texWidth;
NSUInteger height = output->texHeight;
MTLSize threads_per_threadgroup = MTLSizeMake(8, 8, 1);
MTLSize threadgroups = MTLSizeMake((width + threads_per_threadgroup.width - 1) / threads_per_threadgroup.width,
(height + threads_per_threadgroup.height - 1) / threads_per_threadgroup.height, 1);

(height + threads_per_threadgroup.height - 1) / threads_per_threadgroup.height, 1);
id<MTLComputeCommandEncoder> compute_encoder = [command_buffer computeCommandEncoder];
[compute_encoder setBuffer:(__bridge id<MTLBuffer>)constant_buf->impl._buffer offset:0 atIndex:0];
[compute_encoder setAccelerationStructure:_instance_accel atBufferIndex:1];
[compute_encoder setTexture:(__bridge id<MTLTexture>)output->impl._tex atIndex:0];

for (id<MTLAccelerationStructure> primitive_accel in _primitive_accels)
for (id<MTLAccelerationStructure> primitive_accel in _primitive_accels) {
[compute_encoder useResource:primitive_accel usage:MTLResourceUsageRead];

}

[compute_encoder setComputePipelineState:_raytracing_pipeline];
[compute_encoder dispatchThreadgroups:threadgroups threadsPerThreadgroup:threads_per_threadgroup];
[compute_encoder endEncoding];
[command_buffer commit];
}

API_AVAILABLE(macos(11.0))
void kinc_raytrace_copy(kinc_g5_command_list_t *command_list, kinc_g5_render_target_t *target, kinc_g5_texture_t *source) {
id<MTLCommandQueue> queue = getMetalQueue();
id<MTLCommandBuffer> command_buffer = [queue commandBuffer];
Expand Down
36 changes: 0 additions & 36 deletions Backends/System/POSIX/Sources/kinc/backend/atomic.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,6 @@

#include <kinc/global.h>

#if defined(KINC_MACOS) || defined(KINC_IOS)

#include <libkern/OSAtomic.h>

static inline bool kinc_atomic_compare_exchange(volatile int32_t *pointer, int32_t old_value, int32_t new_value) {
return OSAtomicCompareAndSwap32Barrier(old_value, new_value, pointer);
}

static inline bool kinc_atomic_compare_exchange_pointer(void *volatile *pointer, void *old_value, void *new_value) {
return OSAtomicCompareAndSwapPtrBarrier(old_value, new_value, pointer);
}

static inline int32_t kinc_atomic_increment(volatile int32_t *pointer) {
return OSAtomicIncrement32Barrier(pointer) - 1;
}

static inline int32_t kinc_atomic_decrement(volatile int32_t *pointer) {
return OSAtomicDecrement32Barrier(pointer) + 1;
}

static inline void kinc_atomic_exchange(volatile int32_t *pointer, int32_t value) {
__sync_swap(pointer, value);
}

static inline void kinc_atomic_exchange_float(volatile float *pointer, float value) {
__sync_swap((volatile int32_t *)pointer, *(int32_t *)&value);
}

static inline void kinc_atomic_exchange_double(volatile double *pointer, double value) {
__sync_swap((volatile int64_t *)pointer, *(int64_t *)&value);
}

#else

// clang/gcc intrinsics

static inline bool kinc_atomic_compare_exchange(volatile int32_t *pointer, int32_t old_value, int32_t new_value) {
Expand Down Expand Up @@ -86,8 +52,6 @@ static inline void kinc_atomic_exchange_double(volatile double *pointer, double

#endif

#endif

#define KINC_ATOMIC_COMPARE_EXCHANGE(pointer, oldValue, newValue) (kinc_atomic_compare_exchange(pointer, oldValue, newValue))

#define KINC_ATOMIC_COMPARE_EXCHANGE_POINTER(pointer, oldValue, newValue) (kinc_atomic_compare_exchange_pointer(pointer, oldValue, newValue))
Expand Down
22 changes: 11 additions & 11 deletions Backends/System/macOS/Sources/kinc/backend/BasicOpenGLView.m.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,19 +62,19 @@ static bool cmd = false;
cmd = false;
}

if ([theEvent modifierFlags] & NSShiftKeyMask) {
if ([theEvent modifierFlags] & NSEventModifierFlagShift) {
kinc_internal_keyboard_trigger_key_down(KINC_KEY_SHIFT);
shift = true;
}
if ([theEvent modifierFlags] & NSControlKeyMask) {
if ([theEvent modifierFlags] & NSEventModifierFlagControl) {
kinc_internal_keyboard_trigger_key_down(KINC_KEY_CONTROL);
ctrl = true;
}
if ([theEvent modifierFlags] & NSAlternateKeyMask) {
if ([theEvent modifierFlags] & NSEventModifierFlagOption) {
kinc_internal_keyboard_trigger_key_down(KINC_KEY_ALT);
alt = true;
}
if ([theEvent modifierFlags] & NSCommandKeyMask) {
if ([theEvent modifierFlags] & NSEventModifierFlagCommand) {
kinc_internal_keyboard_trigger_key_down(KINC_KEY_META);
cmd = true;
}
Expand Down Expand Up @@ -155,23 +155,23 @@ static bool cmd = false;
kinc_internal_keyboard_trigger_key_press('\t');
break;
default:
if (ch == 'x' && [theEvent modifierFlags] & NSCommandKeyMask) {
if (ch == 'x' && [theEvent modifierFlags] & NSEventModifierFlagCommand) {
char *text = kinc_internal_cut_callback();
if (text != NULL) {
NSPasteboard *board = [NSPasteboard generalPasteboard];
[board clearContents];
[board setString:[NSString stringWithUTF8String:text] forType:NSStringPboardType];
}
}
if (ch == 'c' && [theEvent modifierFlags] & NSCommandKeyMask) {
if (ch == 'c' && [theEvent modifierFlags] & NSEventModifierFlagCommand) {
char *text = kinc_internal_copy_callback();
if (text != NULL) {
NSPasteboard *board = [NSPasteboard generalPasteboard];
[board clearContents];
[board setString:[NSString stringWithUTF8String:text] forType:NSStringPboardType];
}
}
if (ch == 'v' && [theEvent modifierFlags] & NSCommandKeyMask) {
if (ch == 'v' && [theEvent modifierFlags] & NSEventModifierFlagCommand) {
NSPasteboard *board = [NSPasteboard generalPasteboard];
NSString *data = [board stringForType:NSStringPboardType];
if (data != nil) {
Expand Down Expand Up @@ -295,7 +295,7 @@ static bool controlKeyMouseButton = false;

- (void)mouseDown:(NSEvent *)theEvent {
// TODO (DK) map [theEvent window] to window id instead of 0
if ([theEvent modifierFlags] & NSControlKeyMask) {
if ([theEvent modifierFlags] & NSEventModifierFlagControl) {
controlKeyMouseButton = true;
kinc_internal_mouse_trigger_press(0, 1, getMouseX(theEvent), getMouseY(theEvent));
}
Expand All @@ -304,7 +304,7 @@ static bool controlKeyMouseButton = false;
kinc_internal_mouse_trigger_press(0, 0, getMouseX(theEvent), getMouseY(theEvent));
}

if ([theEvent subtype] == NSTabletPointEventSubtype) {
if ([theEvent subtype] == NSEventSubtypeTabletPoint) {
kinc_internal_pen_trigger_press(0, getMouseX(theEvent), getMouseY(theEvent), theEvent.pressure);
}
}
Expand All @@ -319,7 +319,7 @@ static bool controlKeyMouseButton = false;
}
controlKeyMouseButton = false;

if ([theEvent subtype] == NSTabletPointEventSubtype) {
if ([theEvent subtype] == NSEventSubtypeTabletPoint) {
kinc_internal_pen_trigger_release(0, getMouseX(theEvent), getMouseY(theEvent), theEvent.pressure);
}
}
Expand All @@ -333,7 +333,7 @@ static bool controlKeyMouseButton = false;
// TODO (DK) map [theEvent window] to window id instead of 0
kinc_internal_mouse_trigger_move(0, getMouseX(theEvent), getMouseY(theEvent));

if ([theEvent subtype] == NSTabletPointEventSubtype) {
if ([theEvent subtype] == NSEventSubtypeTabletPoint) {
kinc_internal_pen_trigger_move(0, getMouseX(theEvent), getMouseY(theEvent), theEvent.pressure);
}
}
Expand Down
2 changes: 1 addition & 1 deletion Backends/System/macOS/Sources/kinc/backend/system.m.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ id getMetalQueue(void) {
#endif

bool kinc_internal_handle_messages(void) {
NSEvent *event = [myapp nextEventMatchingMask:NSAnyEventMask
NSEvent *event = [myapp nextEventMatchingMask:NSEventMaskAny
untilDate:[NSDate distantPast]
inMode:NSDefaultRunLoopMode
dequeue:YES]; // distantPast: non-blocking
Expand Down
4 changes: 2 additions & 2 deletions Sources/kinc/graphics1/graphics.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ static inline kinc_g4_texture_filter_t map_texture_filter(kinc_g1_texture_filter
}

kinc_log(KINC_LOG_LEVEL_WARNING, "unhandled kinc_g1_texture_filter_t (%i)", filter);
return KINC_G1_TEXTURE_FILTER_LINEAR;
return KINC_G4_TEXTURE_FILTER_LINEAR;
}

static inline kinc_g4_texture_filter_t map_mipmap_filter(kinc_g1_texture_filter_t filter) {
static inline kinc_g4_mipmap_filter_t map_mipmap_filter(kinc_g1_mipmap_filter_t filter) {
switch (filter) {
case KINC_G1_MIPMAP_FILTER_NONE:
return KINC_G4_MIPMAP_FILTER_NONE;
Expand Down

0 comments on commit 2f16a4a

Please sign in to comment.