From 5dce372d9515447142901ad16d3a058ba412640a Mon Sep 17 00:00:00 2001 From: Hyeonseo Yang Date: Thu, 9 Jan 2025 19:14:03 +0900 Subject: [PATCH] =?UTF-8?q?=E2=9E=96=20Make=20libpng=20optional?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Dockerfile | 30 ++++++----- .../MinecraftEnv/src/main/cpp/CMakeLists.txt | 52 +++++++++---------- .../src/main/cpp/framebuffer_capturer.cpp | 17 +++++- 3 files changed, 58 insertions(+), 41 deletions(-) diff --git a/Dockerfile b/Dockerfile index 79d4780a..0b86dd6c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,22 +1,24 @@ -# Ubuntu 베이스 이미지 선택 -FROM ubuntu:latest +# Select ubuntu base image +FROM ubuntu:22.04 -# 환경 설정 및 필요한 소프트웨어 설치 +# Install Java, Python, Git RUN apt-get update && \ - apt-get install -y openjdk-17-jdk python3-pip git && \ + apt-get install -y openjdk-21-jdk python3-pip git libgl1-mesa-dev libegl1-mesa-dev libglew-dev&& \ apt-get clean -# pip를 사용하여 GitHub에서 Python 패키지 직접 설치 -RUN pip3 install git+https://github.com/yhs0602/CraftGround +RUN python3 --version +RUN pip3 install --upgrade pip +RUN pip3 install cmake -# Python 사이트 패키지 경로를 환경변수에 추가 (pip 패키지 경로를 찾기 위함) -ENV PYTHON_SITE_PACKAGES=/usr/local/lib/python3.*/dist-packages +RUN pip3 install git+https://github.com/yhs0602/CraftGround.git@dev -# craftground/MinecraftEnv 디렉토리로 이동하여 gradlew build 실행 -RUN cd $(find $PYTHON_SITE_PACKAGES -type d -maxdepth 1 -name "craftground")/MinecraftEnv && chmod +x gradlew && ./gradlew build - -# 실험 파일이 추가될 작업 디렉토리 설정 +# Clone test repository WORKDIR /workspace +RUN echo "Cloning repository" +RUN git clone https://github.com/yhs0602/minecraft-simulator-benchmark.git -# 컨테이너 실행 시 기본 명령 설정 -CMD ["bash"] +# Set work directory and default execution +WORKDIR /workspace/minecraft-simulator-benchmark +RUN pip3 install wandb tensorboard moviepy git+https://github.com/DLR-RM/stable-baselines3.git +RUN WANDB_MODE=offline PYTHONPATH=. python3 experiments/craftground_exp.py --mode raw --image_width 64x64 --load simulation --max-steps 100 +ENTRYPOINT ["bash"] \ No newline at end of file diff --git a/src/craftground/MinecraftEnv/src/main/cpp/CMakeLists.txt b/src/craftground/MinecraftEnv/src/main/cpp/CMakeLists.txt index 8d956479..2c463e2c 100644 --- a/src/craftground/MinecraftEnv/src/main/cpp/CMakeLists.txt +++ b/src/craftground/MinecraftEnv/src/main/cpp/CMakeLists.txt @@ -11,15 +11,15 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS ON) include(FetchContent) FetchContent_Declare( - glm - GIT_REPOSITORY https://github.com/g-truc/glm.git - GIT_TAG bf71a834948186f4097caa076cd2663c69a10e1e #refs/tags/1.0.1 + glm + GIT_REPOSITORY https://github.com/g-truc/glm.git + GIT_TAG bf71a834948186f4097caa076cd2663c69a10e1e #refs/tags/1.0.1 ) FetchContent_MakeAvailable(glm) set(CRAFGROUND_NATIVE_DEBUG $ENV{CRAFGROUND_NATIVE_DEBUG}) -if (CRAFGROUND_NATIVE_DEBUG) +if(CRAFGROUND_NATIVE_DEBUG) message("CRAFGROUND_NATIVE_DEBUG=${CRAFGROUND_NATIVE_DEBUG}") set(CMAKE_BUILD_TYPE Debug) # Set default build type to Debug else() @@ -35,7 +35,7 @@ include_directories(${JNI_INCLUDE_DIRS}) # Find OpenGL find_package(OpenGL REQUIRED) include_directories(${OPENGL_INCLUDE_DIR}) -if (NOT APPLE) +if(NOT APPLE) find_package(GLEW REQUIRED) include_directories(${GLEW_INCLUDE_DIRS}) message(GLEW_INCLUDE_DIRS="${GLEW_INCLUDE_DIRS}") @@ -43,27 +43,22 @@ if (NOT APPLE) endif() # Find png -if (APPLE) - set(CMAKE_FIND_FRAMEWORK NEVER) - message("CMAKE_FIND_FRAMEWORK=${CMAKE_FIND_FRAMEWORK}") +if(APPLE) + set(CMAKE_FIND_FRAMEWORK NEVER) + message("CMAKE_FIND_FRAMEWORK=${CMAKE_FIND_FRAMEWORK}") endif() # Hacky: Set the CMAKE_FIND_FRAMEWORK to LAST to avoid finding mismatching libs/headers set(CMAKE_FIND_FRAMEWORK LAST) # set(PNG_STATIC ON) -find_package(PNG 1.6 REQUIRED) -include_directories(${PNG_INCLUDE_DIRS}) -message(PNG_INCLUDE_DIRS="${PNG_INCLUDE_DIRS}") -message(PNG_LIBRARIES="${PNG_LIBRARIES}") -# find_library(LIBPNG_STATIC libpng16.a) -#if(NOT LIBPNG_STATIC) -# message(FATAL_ERROR "libpng static library not found") -#else() -# message(LIBPNG_STATIC="${LIBPNG_STATIC}") -#endif() -# Find zlib -# find_package(ZLIB REQUIRED) -# include_directories(${ZLIB_INCLUDE_DIRS}) -# find CUDA +find_package(PNG 1.6 QUIET) +if(PNG_FOUND) + include_directories(${PNG_INCLUDE_DIRS}) + message(PNG_INCLUDE_DIRS="${PNG_INCLUDE_DIRS}") + message(PNG_LIBRARIES="${PNG_LIBRARIES}") + add_compile_definitions(HAS_PNG) +else() + message(FATAL_ERROR "libpng not found. ") +endif() find_package(CUDAToolkit QUIET) if(CUDAToolkit_FOUND) @@ -90,18 +85,23 @@ endif() # Add a library target for your native library add_library(native-lib SHARED ${SOURCE_FILES}) -if (CRAFGROUND_NATIVE_DEBUG) +if(CRAFGROUND_NATIVE_DEBUG) message("CRAFGROUND_NATIVE_DEBUG=${CRAFGROUND_NATIVE_DEBUG}") target_compile_options(native-lib PRIVATE -g) # Add debug symbols endif() # Link with JNI and OpenGL libraries -target_link_libraries(native-lib ${JNI_LIBRARIES} ${OPENGL_LIBRARIES} ${PNG_LIBRARIES} ${ZLIB_LIBRARIES} glm::glm) -if (NOT APPLE) +target_link_libraries(native-lib ${JNI_LIBRARIES} ${OPENGL_LIBRARIES} glm::glm) + +if(PNG_FOUND) + target_link_libraries(native-lib ${PNG_LIBRARIES} ${ZLIB_LIBRARIES}) +endif() + +if(NOT APPLE) target_link_libraries(native-lib ${GLEW_LIBRARIES}) endif() -if (APPLE) +if(APPLE) target_link_libraries( native-lib "-framework Metal" diff --git a/src/craftground/MinecraftEnv/src/main/cpp/framebuffer_capturer.cpp b/src/craftground/MinecraftEnv/src/main/cpp/framebuffer_capturer.cpp index 11792e57..f8dfd667 100644 --- a/src/craftground/MinecraftEnv/src/main/cpp/framebuffer_capturer.cpp +++ b/src/craftground/MinecraftEnv/src/main/cpp/framebuffer_capturer.cpp @@ -18,7 +18,10 @@ #include // For strcmp #include + +#ifdef HAS_PNG #include +#endif #include #include @@ -34,6 +37,7 @@ typedef unsigned char ui8; } \ } while (0) +#ifdef HAS_PNG static void PngWriteCallback(png_structp png_ptr, png_bytep data, png_size_t length) { std::vector *p = (std::vector *)png_get_io_ptr(png_ptr); @@ -80,6 +84,7 @@ void WritePngToMemory( png_set_write_fn(p, &out, PngWriteCallback, NULL); png_write_png(p, info_ptr, PNG_TRANSFORM_IDENTITY, NULL); } +#endif // FIXME: Use glGetIntegerv(GL_NUM_EXTENSIONS) then use glGetStringi for // OpenGL 3.0+ @@ -145,7 +150,7 @@ Java_com_kyhsgeekcode_minecraftenv_FramebufferCapturer_initializeGLEW( #endif } -enum EncodingMode { RAW = 0, PNG = 1 }; +enum EncodingMode { RAW = 0, PNG = 1, ZEROCOPY = 2 }; // 16 x 16 bitmap cursor // 0: transparent, 1: white, 2: black @@ -504,6 +509,7 @@ Java_com_kyhsgeekcode_minecraftenv_FramebufferCapturer_captureFramebufferImpl( // make png bytes from the pixels if (encodingMode == PNG) { +#ifdef HAS_PNG std::vector imageBytes; WritePngToMemory( (size_t)targetSizeX, (size_t)targetSizeY, pixels, imageBytes @@ -515,6 +521,15 @@ Java_com_kyhsgeekcode_minecraftenv_FramebufferCapturer_captureFramebufferImpl( imageBytes.size(), reinterpret_cast(imageBytes.data()) ); +#else + // Handle error + env->ThrowNew( + env->FindClass("java/lang/RuntimeException"), + "PNG encoding is not supported on this platform: Could not find " + "libpng" + ); + return nullptr; +#endif } else if (encodingMode == RAW) { env->SetByteArrayRegion( byteArray,