Skip to content

Commit

Permalink
🎨 Clang format objc
Browse files Browse the repository at this point in the history
  • Loading branch information
yhs0602 committed Dec 28, 2024
1 parent ca4ca82 commit 08e7904
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 57 deletions.
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ This is the latest development repository of CraftGround environment.
1. You need to install JDK >= 21
1. Run the following command to install the package.
```shell
pip install git+https://github.com/yhs0602/CraftGround
pip install craftground
```
1. Take a look at the [the demo repository](https://github.com/yhs0602/CraftGround-Baselines3)!
1. Here is a simple example that uses this environment.
Expand Down Expand Up @@ -249,3 +249,14 @@ https://dejavu-fonts.github.io/License.html
1. Edit .proto
2. protoc
3. Edit python files
# Formatting
## Install formatters
```zsh
brew install ktlint clang-format
```
```bash
find . \( -iname '*.h' -o -iname '*.cpp' -o -iname '*.mm' \) | xargs clang-format -i
ktlint '!src/craftground/MinecraftEnv/src/main/java/com/kyhsgeekcode/minecraftenv/proto/**'
```
43 changes: 25 additions & 18 deletions src/cpp/ipc_apple.mm
Original file line number Diff line number Diff line change
@@ -1,55 +1,62 @@
#include <CoreGraphics/CoreGraphics.h>
#import <Metal/Metal.h>
#include <IOSurface/IOSurface.h>
#import <Metal/Metal.h>
#define GL_SILENCE_DEPRECATION
#include "ipc_apple.h"
#include <OpenGL/OpenGL.h>
#include <OpenGL/gl.h>
#include "ipc_apple.h"
#include <stdexcept>

IOSurfaceRef getIOSurfaceFromMachPort(mach_port_t machPort) {
IOSurfaceRef ioSurface = IOSurfaceLookupFromMachPort(machPort);
return ioSurface;
}

id<MTLTexture> createMetalTextureFromIOSurface(id<MTLDevice> device, IOSurfaceRef ioSurface, int width, int height) {
MTLTextureDescriptor* descriptor = [[MTLTextureDescriptor alloc] init];
id<MTLTexture> createMetalTextureFromIOSurface(
id<MTLDevice> device, IOSurfaceRef ioSurface, int width, int height
) {
MTLTextureDescriptor *descriptor = [[MTLTextureDescriptor alloc] init];
descriptor.pixelFormat = MTLPixelFormatRGBA8Unorm;
descriptor.width = width;
descriptor.height = height;
descriptor.usage = MTLTextureUsageShaderRead | MTLTextureUsageShaderWrite;

id<MTLTexture> texture = [device newTextureWithDescriptor:descriptor iosurface:ioSurface plane:0];
id<MTLTexture> texture = [device newTextureWithDescriptor:descriptor
iosurface:ioSurface
plane:0];
return texture;
}

static void deleteDLManagedTensor(DLManagedTensor* self) {
static void deleteDLManagedTensor(DLManagedTensor *self) {
free(self->dl_tensor.shape);
free(self);
}

DLManagedTensor* createDLPackTensor(IOSurfaceRef ioSurface, size_t width, size_t height) {
DLManagedTensor* tensor = (DLManagedTensor *) malloc(sizeof(DLManagedTensor));
DLManagedTensor *
createDLPackTensor(IOSurfaceRef ioSurface, size_t width, size_t height) {
DLManagedTensor *tensor =
(DLManagedTensor *)malloc(sizeof(DLManagedTensor));

tensor->dl_tensor.data = IOSurfaceGetBaseAddress(ioSurface);
tensor->dl_tensor.ndim = 3; // H x W x C
tensor->dl_tensor.shape = (int64_t *) malloc(3 * sizeof(int64_t));
tensor->dl_tensor.ndim = 3; // H x W x C
tensor->dl_tensor.shape = (int64_t *)malloc(3 * sizeof(int64_t));
tensor->dl_tensor.shape[0] = height;
tensor->dl_tensor.shape[1] = width;
tensor->dl_tensor.shape[2] = 4; // RGBA
tensor->dl_tensor.shape[2] = 4; // RGBA
tensor->dl_tensor.strides = NULL;
tensor->dl_tensor.byte_offset = 0;

tensor->dl_tensor.dtype = (DLDataType){ kDLUInt, 8, 1 }; // Unsigned 8-bit integer
tensor->dl_tensor.device = (DLDevice){ kDLMetal, 0 }; // metal gpu
tensor->dl_tensor.dtype =
(DLDataType){kDLUInt, 8, 1}; // Unsigned 8-bit integer
tensor->dl_tensor.device = (DLDevice){kDLMetal, 0}; // metal gpu

// 메모리 해제 삭제자 설정
tensor->deleter = deleteDLManagedTensor;
return tensor;
}


DLManagedTensor* mtl_tensor_from_mach_port(int machPort, int width, int height) {
DLManagedTensor *
mtl_tensor_from_mach_port(int machPort, int width, int height) {
IOSurfaceRef ioSurface = getIOSurfaceFromMachPort((mach_port_t)machPort);
if (!ioSurface) {
throw std::runtime_error("Failed to initialize IOSurface");
Expand All @@ -60,12 +67,12 @@ static void deleteDLManagedTensor(DLManagedTensor* self) {
throw std::runtime_error("Failed to create Metal device");
}

// id<MTLTexture> texture = createMetalTextureFromIOSurface(device, ioSurface, width, height);
// if (!texture) {
// id<MTLTexture> texture = createMetalTextureFromIOSurface(device,
// ioSurface, width, height); if (!texture) {
// throw std::runtime_error("Failed to create Metal texture");
// }

DLManagedTensor* tensor = createDLPackTensor(ioSurface, width, height);
DLManagedTensor *tensor = createDLPackTensor(ioSurface, width, height);

return tensor;
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,58 +8,66 @@
#include "framebuffer_capturer_apple.h"

IOSurfaceRef createSharedIOSurface(int width, int height) {
NSDictionary *surfaceAttributes = @{
(id)kIOSurfaceWidth : @(width),
(id)kIOSurfaceHeight : @(height),
(id)kIOSurfaceBytesPerElement : @(4), // RGBA8
(id)kIOSurfacePixelFormat : @(0x42475241) // 'RGBA'
};
NSDictionary *surfaceAttributes = @{
(id)kIOSurfaceWidth : @(width),
(id)kIOSurfaceHeight : @(height),
(id)kIOSurfaceBytesPerElement : @(4), // RGBA8
(id)kIOSurfacePixelFormat : @(0x42475241) // 'RGBA'
};

return IOSurfaceCreate((CFDictionaryRef)surfaceAttributes);
return IOSurfaceCreate((CFDictionaryRef)surfaceAttributes);
}

static mach_port_t createMachPortForIOSurface(IOSurfaceRef ioSurface) {
mach_port_t machPort = MACH_PORT_NULL;
machPort = IOSurfaceCreateMachPort(ioSurface);
return machPort;
mach_port_t machPort = MACH_PORT_NULL;
machPort = IOSurfaceCreateMachPort(ioSurface);
return machPort;
}

static IOSurfaceRef ioSurface;
static bool initialized = false;
static GLuint textureID;

// TODO: Depth buffer
int initializeIoSurface(int width, int height, void** return_value) {
if (initialized) {
return 0;
}
int initializeIoSurface(int width, int height, void **return_value) {
if (initialized) {
return 0;
}

// If were to use colorAttachment and depthAttachment, they
// should be first converted to GL_TEXTURE_RECTANGLE_ARB, from GL_TEXTURE_2D
// Therefore, use glCopyTexSubImage2D to copy the contents of the framebuffer
// to ARB textures
// If were to use colorAttachment and depthAttachment, they
// should be first converted to GL_TEXTURE_RECTANGLE_ARB, from GL_TEXTURE_2D
// Therefore, use glCopyTexSubImage2D to copy the contents of the
// framebuffer to ARB textures

// Generate a texture
glGenTextures(1, &textureID);
ioSurface = createSharedIOSurface(width, height);
mach_port_t machPort = createMachPortForIOSurface(ioSurface);
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, textureID);
CGLContextObj cglContext = CGLGetCurrentContext();
CGLTexImageIOSurface2D(cglContext, GL_TEXTURE_RECTANGLE_ARB, GL_RGBA, width,
height, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV,
ioSurface, 0);
initialized = true;
const int size = sizeof(machPort);
void* bytes = malloc(size);
if(bytes == NULL) {
return -1;
}
memcpy(bytes, &machPort, size);
*return_value = bytes;
return size;
// Generate a texture
glGenTextures(1, &textureID);
ioSurface = createSharedIOSurface(width, height);
mach_port_t machPort = createMachPortForIOSurface(ioSurface);
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, textureID);
CGLContextObj cglContext = CGLGetCurrentContext();
CGLTexImageIOSurface2D(
cglContext,
GL_TEXTURE_RECTANGLE_ARB,
GL_RGBA,
width,
height,
GL_BGRA,
GL_UNSIGNED_INT_8_8_8_8_REV,
ioSurface,
0
);
initialized = true;
const int size = sizeof(machPort);
void *bytes = malloc(size);
if (bytes == NULL) {
return -1;
}
memcpy(bytes, &machPort, size);
*return_value = bytes;
return size;
}

void copyFramebufferToIOSurface(int width, int height) {
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, textureID);
glCopyTexSubImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, 0, 0, 0, 0, width, height);
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, textureID);
glCopyTexSubImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, 0, 0, 0, 0, width, height);
}

0 comments on commit 08e7904

Please sign in to comment.