Skip to content

Headless renderer #14

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

Merged
merged 50 commits into from
Mar 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
8ad2d3f
merge commit
Rabbidon Dec 3, 2022
6173102
Fixed all issues
neverix Dec 3, 2022
ef83b83
Architecture for screenshotting
neverix Dec 3, 2022
8c3723a
Final additions
neverix Dec 4, 2022
49be8b5
Fix color bug again
neverix Dec 4, 2022
59a83a5
One weird trick
neverix Dec 5, 2022
10af879
..debugging
neverix Dec 5, 2022
772ae82
Remove headless code
neverix Dec 5, 2022
3fa414c
Make everything work
neverix Dec 8, 2022
43f69bc
Remove debugging code
neverix Dec 8, 2022
37f4282
Changes
neverix Dec 8, 2022
67ba452
Merge branch 'headless-renderer' into develop
neverix Dec 8, 2022
467ce1f
Test adding irrlicht
neverix Dec 8, 2022
724bb7c
Make it build
neverix Dec 8, 2022
c2ede77
Recreate setup
neverix Dec 8, 2022
0c9e65b
Merged
neverix Dec 8, 2022
07ad613
Add irrlichtmt
neverix Dec 8, 2022
f76587f
Everything works
neverix Dec 8, 2022
f1882c9
Change submodule path
neverix Dec 8, 2022
610c7f0
Update renderingengine.cpp
neverix Dec 9, 2022
7358d6b
Try to fix memory leak
neverix Dec 11, 2022
25e0cce
Revert "Try to fix memory leak"
neverix Dec 12, 2022
65b624e
Remove rendering
neverix Dec 12, 2022
b3a7477
Make headlessness a flag
neverix Dec 17, 2022
b33b8ec
Make it partially work
neverix Dec 17, 2022
0a9a351
include no SDL headers if no headless build
Rabbidon Jan 12, 2023
4b70f36
Add IMPORTED target for SDL if none exported
JosiahWI Jan 12, 2023
b11d2d8
Fix memory leak in render core
JosiahWI Jan 12, 2023
564b4ba
Revert commit from other branch
JosiahWI Jan 12, 2023
078108a
Merge pull request #26 from JosiahWI/fix/render-core-memleak
neverix Jan 12, 2023
7c3effd
Revert revert commit
neverix Jan 12, 2023
691eb6f
Do not delete buffer we do not own
JosiahWI Jan 12, 2023
e615079
Merge pull request #28 from JosiahWI/fix/render-core-bad-delete
Rabbidon Jan 12, 2023
ad86c4c
Define BUILD_HEADLESS symbol in cmake_config.h.in
JosiahWI Jan 12, 2023
2c33126
Remove duplicate find_package call for SDL2
JosiahWI Jan 24, 2023
7b01b1b
it compiles
Jan 25, 2023
3ffe1ad
fix SDL2 dependencies
Jan 26, 2023
7ff403c
Merge pull request #38 from AI-WAIFU/zmqpp-headless
AI-WAIFU Jan 27, 2023
d21895c
Fix SDL2 target not found
rk1a Feb 1, 2023
fc5b597
Set video_driver=null in minetest.conf
rk1a Feb 1, 2023
e93b26a
Disable shaders in minetest.conf
rk1a Feb 1, 2023
2f3ce0f
Merge pull request #29 from JosiahWI/fix/build-headless-symbol
AI-WAIFU Feb 9, 2023
8161954
Merge pull request #41 from EleutherAI/cmake-SDL2-target-fix
rk1a Feb 9, 2023
53ca098
merge develop into headless renderer
Feb 9, 2023
95fa15a
Merge pull request #52 from AI-WAIFU/merge-develop
AI-WAIFU Feb 9, 2023
b8913a3
add build_instructions
AI-WAIFU Mar 19, 2023
191dec2
merge develop into headless render
AI-WAIFU Mar 19, 2023
1d0bf70
mod build instructions after merge
AI-WAIFU Mar 19, 2023
ac11d8c
Merge pull request #60 from AI-WAIFU/merge-headless-renderer
AI-WAIFU Mar 19, 2023
6d25eb5
incorporate feedback
AI-WAIFU Mar 19, 2023
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,8 @@ compile_commands.json
.vs/

# Optional user provided library folder
lib/irrlichtmt
# TODO: don't add irrlichtmt, apply patch from CMakeLists.txt

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't merge TODO comments.

# lib/irrlichtmt

# Generated mod storage database
client/mod_storage.sqlite
5 changes: 5 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
[submodule "lib/irrlichtmt"]
path = lib/irrlichtmt
url = [email protected]:EleutherAI/irrlicht.git
branch = headless-rendering

[submodule "lib/zmqpp"]
path = lib/zmqpp
url = [email protected]:zeromq/zmqpp.git
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ set(RUN_IN_PLACE ${DEFAULT_RUN_IN_PLACE} CACHE BOOL


set(BUILD_CLIENT TRUE CACHE BOOL "Build client")
set(BUILD_HEADLESS TRUE CACHE BOOL "Build in headless mode")
set(BUILD_SERVER FALSE CACHE BOOL "Build server")
set(BUILD_UNITTESTS TRUE CACHE BOOL "Build unittests")
set(BUILD_BENCHMARKS FALSE CACHE BOOL "Build benchmarks")
Expand All @@ -67,7 +68,6 @@ set(ENABLE_UPDATE_CHECKER (NOT ${DEVELOPMENT_BUILD}) CACHE BOOL
# Included stuff
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules/")


set(IRRLICHTMT_BUILD_DIR "" CACHE PATH "Path to IrrlichtMt build directory.")
if(NOT "${IRRLICHTMT_BUILD_DIR}" STREQUAL "")
find_package(IrrlichtMt QUIET
Expand Down
49 changes: 49 additions & 0 deletions build_instructions.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
1. Install prereqs
1. sudo apt-get install xvfb g++ make libzmq3-dev libtool pkg-config build-essential autoconf automake libc6-dev cmake libpng-dev libjpeg-dev libxi-dev libgl1-mesa-dev libsqlite3-dev libogg-dev libvorbis-dev libopenal-dev libcurl4-gnutls-dev libfreetype6-dev zlib1g-dev libgmp-dev libjsoncpp-dev libzstd-dev libluajit-5.1-dev protobuf-compiler

2. Build SDL2
1. clone the SDL2 repo https://github.com/libsdl-org/SDL
2. checkout release-2.26.2 https://github.com/libsdl-org/SDL/tree/release-2.26.2
3. create a build directory inside the SDL repo
4. cd into it and run ../configure --prefix=/path/to/SDL/dir/build && make && make install

3. Build zmqpp
1. clone https://github.com/zeromq/zmqpp
2. checkout the develop branch
3. run make

4. Clone EAI alignment minetest repos
1. clone https://github.com/EleutherAI/minetest
2. checkout the develop branch
3. clone https://github.com/EleutherAI/irrlicht
4. checkout headless-renderer

5. Clone minetest game spec
1. clone https://github.com/minetest/minetest_game
2. checkout master branch

5. Establish symlinks
1. cd into minetest/lib
2. rm -r zmqpp irrlichtmt
3. ln -s ../../zmqpp/ zmqpp
4. ln -s ../../irrlicht/ irrlichtmt
5. cd into minetest/games
6. ln -s ../../minetest_game/ minetest_game

6. Build minetest
1. cd into minetest
2. either run
cmake . -DRUN_IN_PLACE=TRUE -DBUILD_HEADLESS=1 -DSDL2_DIR=<path to SDL repo>/SDL/build/lib/cmake/SDL2/
or
cmake . -DRUN_IN_PLACE=TRUE -DBUILD_HEADLESS=0 -DSDL2_DIR=
3. run make -j$(nproc)

7. setup python
1. create a new python conda env or venv (tested with python3.9)
2. pip install gym matplotlib protobuf==3.20.1 psutil zmq
3. cd into the scripts directory and run compile_proto.sh
4. run python -m minetester.scripts.test_loop




1 change: 1 addition & 0 deletions lib/irrlichtmt
Submodule irrlichtmt added at 3b7fa4
5 changes: 0 additions & 5 deletions minetest.conf

This file was deleted.

4 changes: 3 additions & 1 deletion minetester/minetest_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,8 @@ def _write_config(self):
config_file.write("mute_sound = true\n")
config_file.write("show_debug = false\n")
config_file.write("enable_client_modding = true\n")
#config_file.write("video_driver = null\n")
#config_file.write("enable_shaders = false\n")
config_file.write("csm_restriction_flags = 0\n")
config_file.write("enable_mod_channels = true\n")

Expand Down Expand Up @@ -308,7 +310,7 @@ def step(self, action: Dict[str, Any]):

self.last_obs = next_obs
logging.debug(f"Received obs - {next_obs.shape}; reward - {rew}")
return next_obs, rew, done, info
return next_obs, rew, info

def render(self, render_mode: str = "human"):
if render_mode == "human":
Expand Down
4 changes: 2 additions & 2 deletions minetester/scripts/test_loop.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
clientmods=["random_v0"],
)

render = True
render = True
obs = env.reset()
done = False
while not done:
try:
action = env.action_space.sample()
obs, rew, done, info = env.step(action)
obs, rew, info = env.step(action)
if render:
env.render()
except KeyboardInterrupt:
Expand Down
20 changes: 19 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,17 @@ set(CMAKE_BUILD_TYPE "${CMAKE_BUILD_TYPE}" CACHE STRING
# Set some random things default to not being visible in the GUI
mark_as_advanced(EXECUTABLE_OUTPUT_PATH LIBRARY_OUTPUT_PATH)

if(BUILD_CLIENT AND BUILD_HEADLESS)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should not be in both CMakeLists.txt and src/CMakeLists.txt.

find_package(SDL2 REQUIRED NO_DEFAULT_PATH)
# SDL2 exports targets since 2.0.6, but some distributions override config.
if(NOT TARGET SDL2::SDL2)
add_library(SDL2::SDL2 INTERFACE IMPORTED)
set_target_properties(SDL2::SDL2 PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES ${SDL2_INCLUDE_DIRS}
INTERFACE_LINK_LIBRARIES ${SDL2_LIBRARIES}
)
endif()
endif()

if(NOT (BUILD_CLIENT OR BUILD_SERVER))
message(WARNING "Neither BUILD_CLIENT nor BUILD_SERVER is set! Setting BUILD_SERVER=true")
Expand Down Expand Up @@ -64,7 +75,6 @@ if(NOT USE_CURL)
endif()
endif()


option(ENABLE_GETTEXT "Use GetText for internationalization" ${BUILD_CLIENT})
set(USE_GETTEXT FALSE)

Expand Down Expand Up @@ -528,10 +538,12 @@ include_directories(SYSTEM
${ZMQ_INCLUDE_DIR}
${ZMQPP_INCLUDE_DIR}
${SQLITE3_INCLUDE_DIR}
${SDL2_INCLUDE_DIR}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not needed.

${LUA_INCLUDE_DIR}
${GMP_INCLUDE_DIR}
${JSON_INCLUDE_DIR}
${LUA_BIT_INCLUDE_DIR}
${SDL2_INCLUDE_DIR}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not needed.

)

if(USE_GETTEXT)
Expand Down Expand Up @@ -560,16 +572,22 @@ endif()
if(BUILD_CLIENT)
add_executable(${PROJECT_NAME} ${client_SRCS} ${extra_windows_SRCS})
add_dependencies(${PROJECT_NAME} GenerateVersion)

if(BUILD_HEADLESS)
set(SDL2_TARGET SDL2::SDL2)
endif()
target_link_libraries(
${PROJECT_NAME}
${ZLIB_LIBRARIES}
IrrlichtMt::IrrlichtMt
${SDL2_TARGET}
${ZSTD_LIBRARY}
${ZMQ_LIBRARY}
${ZMQPP_LIBRARY}
${X11_LIBRARIES}
${SOUND_LIBRARIES}
${SQLITE3_LIBRARY}
${SDL2_LIBRARY}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not needed.

${LUA_LIBRARY}
${GMP_LIBRARY}
${JSON_LIBRARY}
Expand Down
5 changes: 4 additions & 1 deletion src/client/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#include "cmake_config.h"
#include <iostream>
#include <algorithm>
#include <sstream>
Expand Down Expand Up @@ -1825,6 +1826,7 @@ void Client::makeScreenshot()
{
irr::video::IVideoDriver *driver = m_rendering_engine->get_video_driver();
irr::video::IImage* raw_image;

if(m_rendering_engine->headless) {
raw_image = m_rendering_engine->get_screenshot();
} else {
Expand Down Expand Up @@ -1946,6 +1948,7 @@ bool Client::getTerminal() {

pb_objects::Image Client::getSendableData(core::position2di cursorPosition, bool isMenuActive, irr::video::IImage* cursorImage) {
irr::video::IVideoDriver *driver = m_rendering_engine->get_video_driver();

irr::video::IImage* raw_image;
if(m_rendering_engine->headless) {
raw_image = m_rendering_engine->get_screenshot();
Expand All @@ -1959,7 +1962,6 @@ pb_objects::Image Client::getSendableData(core::position2di cursorPosition, bool
irr::video::IImage* const image =
driver->createImage(video::ECF_R8G8B8, raw_image->getDimension());
raw_image->copyTo(image);
raw_image->drop();

// if provided draw the cursor image at the current mouse position when GUI is open
if (isMenuActive && cursorImage) {
Expand All @@ -1975,6 +1977,7 @@ pb_objects::Image Client::getSendableData(core::position2di cursorPosition, bool
pb_img.set_width(dim.Width);
pb_img.set_height(dim.Height);
image->drop();
raw_image->drop();
return pb_img;
}

Expand Down
3 changes: 1 addition & 2 deletions src/client/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1186,7 +1186,7 @@ bool Game::startup(bool *kill,
if (start_data.isDumbClient()) {
dynamic_cast<DumbClientInputHandler*>(input)->socket = zmqclient;
}
if (start_data.record) {
if (start_data.isRecording()) {
createRecorder(start_data);
recorder->sender = zmqclient;
}
Expand Down Expand Up @@ -1233,7 +1233,6 @@ void Game::run()
while (m_rendering_engine->run() &&
!(*kill || g_gamecallback->shutdown_requested ||
(server && server->isShutdownRequested()))) {

// send data out
std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now();
if(recorder && !firstIter) {
Expand Down
5 changes: 2 additions & 3 deletions src/client/render/core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#include "cmake_config.h"
#include "core.h"
#include "plain.h"
#include "client/shadows/dynamicshadowsrender.h"
Expand Down Expand Up @@ -64,8 +65,6 @@ void RenderingCore::draw(video::SColor _skycolor, bool _show_hud, bool _show_min
buffer->setTexture(0, v2f(1.0f, 1.0f), "idk_lol", video::ECF_R8G8B8);
auto tex = new TextureBufferOutput(buffer, 0);
pipeline->setRenderTarget(tex);


pipeline->reset(context);
pipeline->run(context);

Expand Down Expand Up @@ -105,4 +104,4 @@ video::IImage *RenderingCore::get_screenshot() {
v2u32 RenderingCore::getVirtualSize() const
{
return virtual_size;
}
}
1 change: 1 addition & 0 deletions src/client/render/pipeline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#include "cmake_config.h"
#include "pipeline.h"
#include "client/client.h"
#include "client/hud.h"
Expand Down
1 change: 1 addition & 0 deletions src/client/render/plain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#include "cmake_config.h"
#include "plain.h"
#include "secondstage.h"
#include "client/camera.h"
Expand Down
9 changes: 9 additions & 0 deletions src/client/renderingengine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#include "cmake_config.h"
#include <IrrlichtDevice.h>
#include "fontengine.h"
#include "client.h"
Expand All @@ -35,6 +36,9 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "inputhandler.h"
#include "gettext.h"
#include "../gui/guiSkin.h"
#if BUILD_HEADLESS
#include <SDL_video.h>
#endif

#if !defined(_WIN32) && !defined(__APPLE__) && !defined(__ANDROID__) && \
!defined(SERVER) && !defined(__HAIKU__)
Expand Down Expand Up @@ -141,6 +145,10 @@ RenderingEngine::RenderingEngine(IEventReceiver *receiver)
params.OGLES2ShaderPath = (porting::path_share + DIR_DELIM + rel_path + DIR_DELIM).c_str();
#endif

#if BUILD_HEADLESS
SDL_VideoInit("offscreen");
#endif

m_device = createDeviceEx(params);
driver = m_device->getVideoDriver();

Expand Down Expand Up @@ -655,3 +663,4 @@ float RenderingEngine::getDisplayDensity()
}

#endif // __ANDROID__

1 change: 1 addition & 0 deletions src/cmake_config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#define STATIC_LOCALEDIR "@LOCALEDIR@"
#define BUILD_TYPE "@CMAKE_BUILD_TYPE@"
#define ICON_DIR "@ICONDIR@"
#cmakedefine01 BUILD_HEADLESS
#cmakedefine01 RUN_IN_PLACE
#cmakedefine01 DEVELOPMENT_BUILD
#cmakedefine01 ENABLE_UPDATE_CHECKER
Expand Down
1 change: 1 addition & 0 deletions src/gameparams.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ struct GameStartData : GameParams

bool isSinglePlayer() const { return address.empty() && !local_server; }
bool isDumbClient() const { return dumb && !client_address.empty(); }
bool isRecording() const { return record && !client_address.empty(); }
bool isResizable() const { return resizable; }
bool isHeadless() const { return headless; }

Expand Down
Empty file added tmp.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.