Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

➖ Make libpng optional #19

Merged
merged 1 commit into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 16 additions & 14 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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"]
52 changes: 26 additions & 26 deletions src/craftground/MinecraftEnv/src/main/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -35,35 +35,30 @@ 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}")
message(GLEW_LIBRARIES="${GLEW_LIBRARIES}")
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)
Expand All @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@

#include <cstring> // For strcmp
#include <iostream>

#ifdef HAS_PNG
#include <png.h>
#endif
#include <stdlib.h>
#include <vector>

Expand All @@ -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<ui8> *p = (std::vector<ui8> *)png_get_io_ptr(png_ptr);
Expand Down Expand Up @@ -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+
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -504,6 +509,7 @@ Java_com_kyhsgeekcode_minecraftenv_FramebufferCapturer_captureFramebufferImpl(

// make png bytes from the pixels
if (encodingMode == PNG) {
#ifdef HAS_PNG
std::vector<ui8> imageBytes;
WritePngToMemory(
(size_t)targetSizeX, (size_t)targetSizeY, pixels, imageBytes
Expand All @@ -515,6 +521,15 @@ Java_com_kyhsgeekcode_minecraftenv_FramebufferCapturer_captureFramebufferImpl(
imageBytes.size(),
reinterpret_cast<jbyte *>(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,
Expand Down
Loading