Skip to content

Commit

Permalink
add python api
Browse files Browse the repository at this point in the history
  • Loading branch information
jkxing committed Jan 17, 2024
1 parent fb0bbfd commit 7d5b7fc
Show file tree
Hide file tree
Showing 32 changed files with 1,214 additions and 8 deletions.
6 changes: 6 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,9 @@
[submodule "src/ext/json"]
path = src/ext/json
url = https://github.com/nlohmann/json.git
[submodule "src/ext/pybind11"]
path = src/ext/pybind11
url = https://github.com/pybind/pybind11.git
[submodule "src/ext/dlpack"]
path = src/ext/dlpack
url = https://github.com/dmlc/dlpack.git
18 changes: 18 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,22 @@ endif ()
set(CMAKE_FIND_PACKAGE_SORT_ORDER NATURAL)
set(CMAKE_FIND_PACKAGE_SORT_DIRECTION DEC)

if (SKBUILD)
set(CMAKE_INSTALL_INCLUDEDIR ${SKBUILD_NULL_DIR})
set(CMAKE_INSTALL_DOCDIR ${SKBUILD_NULL_DIR})
if (WIN32)
set(CMAKE_INSTALL_LIBDIR ${SKBUILD_NULL_DIR})
else ()
set(CMAKE_INSTALL_LIBDIR ${SKBUILD_PLATLIB_DIR}/luisarender/dylibs)
endif ()
set(CMAKE_INSTALL_BINDIR ${SKBUILD_PLATLIB_DIR}/luisarender/dylibs)
else ()
include(GNUInstallDirs)
endif ()

if (CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
add_compile_options("/wd4996")
endif ()

add_subdirectory(src)

89 changes: 89 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
[build-system]
requires = ["pybind11", "scikit-build-core>=0.3.2", "ninja"]
build-backend = "scikit_build_core.build"

[project]
name = "luisarender-python"
version = "0.0.2"
description = "A High-Performance Rendering Framework with Layered and Unified Interfaces on Stream Architectures"
readme = "README.md"
authors = [
{ name = "LuisaGroup" },
]
homepage = "luisa-render.com"
repository = "https://github.com/LuisaGroup/LuisaRender"
requires-python = ">=3.10"
classifiers = [
"Development Status :: 4 - Beta",
"License :: OSI Approved :: BSD License",
"Programming Language :: Python :: 3 :: Only",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
]

license = { file = "LICENSE" }

[tool.scikit-build]
cmake.minimum-version = "3.18"
cmake.build-type = "Release"
cmake.args = ["-GNinja","-Wno-dev"]
sdist.reproducible = false
wheel.expand-macos-universal-tags = true
wheel.packages = []
build-dir = "build/{wheel_tag}"

[project.optional-dependencies]
test = ["pytest"]

[tool.pytest.ini_options]
minversion = "6.0"
addopts = ["-ra", "--showlocals", "--strict-markers", "--strict-config"]
xfail_strict = true
log_cli_level = "INFO"
filterwarnings = [
"error",
]
testpaths = ["tests"]

[tool.cibuildwheel]
test-command = "pytest {project}/tests"
test-extras = ["test"]
test-skip = ["*universal2:arm64"]
build-verbosity = 1

[tool.ruff]
src = ["src"]

[tool.ruff.lint]
extend-select = [
"B", # flake8-bugbear
"I", # isort
"ARG", # flake8-unused-arguments
"C4", # flake8-comprehensions
"EM", # flake8-errmsg
"ICN", # flake8-import-conventions
"G", # flake8-logging-format
"PGH", # pygrep-hooks
"PIE", # flake8-pie
"PL", # pylint
"PT", # flake8-pytest-style
"PTH", # flake8-use-pathlib
"RET", # flake8-return
"RUF", # Ruff-specific
"SIM", # flake8-simplify
"T20", # flake8-print
"UP", # pyupgrade
"YTT", # flake8-2020
"EXE", # flake8-executable
"NPY", # NumPy specific rules
"PD", # pandas-vet
]
ignore = [
"PLR09", # Too many X
"PLR2004", # Magic comparison
]
isort.required-imports = ["from __future__ import annotations"]

[tool.ruff.lint.per-file-ignores]
"tests/**" = ["T20"]
13 changes: 13 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,12 @@ endif ()
set(LUISA_COMPUTE_ENABLE_LTO OFF CACHE BOOL "" FORCE)
set(LUISA_COMPUTE_ENABLE_PYTHON OFF CACHE BOOL "" FORCE)
set(LUISA_COMPUTE_ENABLE_UNITY_BUILD ${LUISA_RENDER_ENABLE_UNITY_BUILD} CACHE BOOL "" FORCE)

set(SKBUILD_OLD ${SKBUILD})
set(SKBUILD OFF)
add_subdirectory(compute)
set(SKBUILD ${SKBUILD_OLD})

target_include_directories(luisa-render-include INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/compute/include/luisa)

add_subdirectory(sdl)
Expand All @@ -55,6 +60,10 @@ function(luisa_render_add_plugin name)
set_target_properties(${lib_name} PROPERTIES
UNITY_BUILD OFF
DEBUG_POSTFIX "")
install(TARGETS ${lib_name}
LIBRARY DESTINATION ${CMAKE_INSTALL_BINDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
endfunction()

add_subdirectory(films)
Expand Down Expand Up @@ -103,3 +112,7 @@ add_subdirectory(apps)
if (LUISA_RENDER_BUILD_TESTS)
add_subdirectory(tests)
endif ()

if (SKBUILD)
add_subdirectory(python)
endif ()
2 changes: 1 addition & 1 deletion src/apps/cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ using namespace luisa::render;

int main(int argc, char *argv[]) {

log_level_info();
log_level_verbose();
luisa::compute::Context context{argv[0]};
auto macros = parse_cli_macros(argc, argv);
for (auto &&[k, v] : macros) {
Expand Down
5 changes: 5 additions & 0 deletions src/base/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,8 @@ target_link_libraries(luisa-render-base PUBLIC
set_target_properties(luisa-render-base PROPERTIES
WINDOWS_EXPORT_ALL_SYMBOLS ON
UNITY_BUILD ${LUISA_RENDER_ENABLE_UNITY_BUILD})

install(TARGETS luisa-render-base
LIBRARY DESTINATION ${CMAKE_INSTALL_BINDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
1 change: 1 addition & 0 deletions src/base/film.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class Film : public SceneNode {
virtual void clear(CommandBuffer &command_buffer) noexcept = 0;
virtual void download(CommandBuffer &command_buffer, float4 *framebuffer) const noexcept = 0;
virtual bool show(CommandBuffer &command_buffer) const noexcept { return false; }
virtual void *export_image(CommandBuffer &command_buffer) { return nullptr; }
virtual void release() noexcept = 0;
};

Expand Down
16 changes: 16 additions & 0 deletions src/base/integrator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,22 @@ void ProgressiveIntegrator::Instance::render(Stream &stream) noexcept {
}
}

luisa::vector<void*> ProgressiveIntegrator::Instance::render_with_return(Stream &stream) noexcept {
CommandBuffer command_buffer{&stream};
luisa::vector<void*> result;
for (auto i = 0u; i < pipeline().camera_count(); i++) {
auto camera = pipeline().camera(i);
auto resolution = camera->film()->node()->resolution();
auto pixel_count = resolution.x * resolution.y;
camera->film()->prepare(command_buffer);
_render_one_camera(command_buffer, camera);
command_buffer << compute::synchronize();
result.push_back(camera->film()->export_image(command_buffer));
}
return result;
}


void ProgressiveIntegrator::Instance::_render_one_camera(
CommandBuffer &command_buffer, Camera::Instance *camera) noexcept {

Expand Down
5 changes: 5 additions & 0 deletions src/base/integrator.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ class Integrator : public SceneNode {
[[nodiscard]] auto light_sampler() noexcept { return _light_sampler.get(); }
[[nodiscard]] auto light_sampler() const noexcept { return _light_sampler.get(); }
virtual void render(Stream &stream) noexcept = 0;
virtual luisa::vector<void*> render_with_return(Stream &stream) {
LUISA_INFO("Not implemented!");
return luisa::vector<void *>{};
}
};

private:
Expand Down Expand Up @@ -72,6 +76,7 @@ class ProgressiveIntegrator : public Integrator {
const ProgressiveIntegrator *node) noexcept;
~Instance() noexcept override;
void render(Stream &stream) noexcept override;
luisa::vector<void*> render_with_return(Stream &stream) noexcept override;
};

public:
Expand Down
31 changes: 31 additions & 0 deletions src/base/pipeline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,29 @@ bool Pipeline::update(CommandBuffer &command_buffer, float time) noexcept {
return updated;
}


void Pipeline::update_texture(Stream &stream, uint texture_id, float4 new_texture) noexcept {
auto device_float = make_float4(new_texture);
LUISA_INFO("LuisaRender Create device_buffer");
_textures.begin()->second->update_by_buffer(stream, device_float);
}

void Pipeline::update_mesh(uint mesh_id, uint64_t vertex_buffer) noexcept {
// if (auto iter = _geometry->instances().find(mesh_id); iter != _geometry->instances().end()) {
// iter->second->update(vertex_buffer);
// //return true;
// }
//return false;
}

void Pipeline::render(Stream &stream) noexcept {
_integrator->render(stream);
}

luisa::vector<void*> Pipeline::render_with_return(Stream &stream) noexcept {
return _integrator->render_with_return(stream);
}

const Texture::Instance *Pipeline::build_texture(CommandBuffer &command_buffer, const Texture *texture) noexcept {
if (texture == nullptr) { return nullptr; }
if (auto iter = _textures.find(texture); iter != _textures.end()) {
Expand Down Expand Up @@ -186,4 +205,16 @@ Float4 Pipeline::constant(Expr<uint> index) const noexcept {
return _constant_buffer->read(index);
}

void Pipeline::update_constant(Stream &stream, uint index, float4 new_value) const noexcept {
if(!_constant_buffer) {
LUISA_INFO("Pipeline::update_constant: constant_buffer is nullptr");
return;
}
LUISA_INFO("{} {} {}",index, new_value, _constant_buffer.size());
stream << _constant_buffer.view(index, 1u).copy_from(&new_value) << compute::commit();
LUISA_INFO("Pipeline::update_constant");
}



}// namespace luisa::render
4 changes: 4 additions & 0 deletions src/base/pipeline.h
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,10 @@ class Pipeline {
[[nodiscard]] const Filter::Instance *build_filter(CommandBuffer &command_buffer, const Filter *filter) noexcept;
[[nodiscard]] const PhaseFunction::Instance *build_phasefunction(CommandBuffer &command_buffer, const PhaseFunction *phasefunction) noexcept;
bool update(CommandBuffer &command_buffer, float time) noexcept;
void update_texture(Stream &stream, uint texture_id, float4 new_value) noexcept;
void update_mesh(uint mesh_id, uint64_t vertex_buffer) noexcept;
void render(Stream &stream) noexcept;
luisa::vector<void*> render_with_return(Stream &stream) noexcept;
[[nodiscard]] auto &printer() noexcept { return *_printer; }
[[nodiscard]] auto &printer() const noexcept { return *_printer; }
[[nodiscard]] uint named_id(luisa::string_view name) const noexcept;
Expand All @@ -234,6 +237,7 @@ class Pipeline {
[[nodiscard]] Float4x4 transform(const Transform *transform) const noexcept;

[[nodiscard]] Float4 constant(Expr<uint> index) const noexcept;
void update_constant(Stream &stream, uint index, float4 new_value) const noexcept;

template<uint dim, typename... Args, typename... CallArgs>
[[nodiscard]] auto shader(luisa::string_view name, CallArgs &&...call_args) const noexcept {
Expand Down
6 changes: 5 additions & 1 deletion src/base/texture.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ class Texture : public SceneNode {
class Instance {

private:
const Pipeline &_pipeline;
const Texture *_texture;
const Pipeline &_pipeline;

protected:
[[nodiscard]] Spectrum::Decode _evaluate_static_albedo_spectrum(
Expand All @@ -57,6 +57,10 @@ class Texture : public SceneNode {
const Interaction &it, const SampledWavelengths &swl, Expr<float> time) const noexcept;
[[nodiscard]] virtual Spectrum::Decode evaluate_illuminant_spectrum(
const Interaction &it, const SampledWavelengths &swl, Expr<float> time) const noexcept;
virtual void update_by_buffer(Stream &stream, float4 texture_buffer){
LUISA_WARNING_WITH_LOCATION(
"Texture::update_by_buffer() is not implemented for this texture type.");
}
};

public:
Expand Down
2 changes: 1 addition & 1 deletion src/compute
Submodule compute updated 300 files
22 changes: 22 additions & 0 deletions src/ext/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ add_library(fast_float INTERFACE)
target_include_directories(fast_float INTERFACE fast_float/include)
target_link_libraries(luisa-render-ext INTERFACE fast_float)

install(TARGETS luisa-render-ext
LIBRARY DESTINATION ${CMAKE_INSTALL_BINDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})

# shared by assimp and tinyexr
find_package(ZLIB)

Expand All @@ -23,6 +28,8 @@ if (ZLIB_FOUND)
else ()
set(ASSIMP_BUILD_ZLIB ON CACHE BOOL "" FORCE)
endif ()

set(ASSIMP_BIN_INSTALL_DIR ${CMAKE_INSTALL_BINDIR})
set(ASSIMP_BUILD_ASSIMP_TOOLS OFF CACHE BOOL "" FORCE)
set(ASSIMP_BUILD_TESTS OFF CACHE BOOL "" FORCE)
set(ASSIMP_INSTALL ON CACHE BOOL "" FORCE)
Expand Down Expand Up @@ -52,6 +59,11 @@ else ()
target_compile_definitions(tinyexr PUBLIC TINYEXR_USE_MINIZ=1)
target_include_directories(tinyexr PRIVATE tinyexr/deps/miniz)
endif ()

install(TARGETS tinyexr
LIBRARY DESTINATION ${CMAKE_INSTALL_BINDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
target_include_directories(tinyexr PUBLIC tinyexr)
target_link_libraries(tinyexr PUBLIC ${CMAKE_DL_LIBS})
set_target_properties(tinyexr PROPERTIES WINDOWS_EXPORT_ALL_SYMBOLS ON)
Expand All @@ -61,3 +73,13 @@ target_link_libraries(luisa-render-ext INTERFACE tinyexr)
add_library(nlohmann_json INTERFACE)
target_include_directories(nlohmann_json INTERFACE json/single_include)
target_link_libraries(luisa-render-ext INTERFACE nlohmann_json)


install(TARGETS nlohmann_json
LIBRARY DESTINATION ${CMAKE_INSTALL_BINDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})

if (SKBUILD OR LUISA_COMPUTE_FORCE_PYTHON_BINDINGS)
add_subdirectory(pybind11)
endif ()
1 change: 1 addition & 0 deletions src/ext/dlpack
Submodule dlpack added at 2a7e9f
1 change: 1 addition & 0 deletions src/ext/pybind11
Submodule pybind11 added at f29def
10 changes: 8 additions & 2 deletions src/films/color.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ class ColorFilmInstance final : public Film::Instance {
public:
ColorFilmInstance(Device &device, Pipeline &pipeline, const ColorFilm *film) noexcept;
void prepare(CommandBuffer &command_buffer) noexcept override;
void* export_image(CommandBuffer &command_buffer) override {
_check_prepared();
auto resolution = node()->resolution();
auto pixel_count = resolution.x * resolution.y;
command_buffer << _convert_image.get()(_image, _converted).dispatch(pixel_count);
command_buffer << compute::synchronize();
return _converted.native_handle();
}
void download(CommandBuffer &command_buffer, float4 *framebuffer) const noexcept override;
[[nodiscard]] Film::Accumulation read(Expr<uint2> pixel) const noexcept override;
void release() noexcept override;
Expand All @@ -76,14 +84,12 @@ class ColorFilmInstance final : public Film::Instance {

ColorFilmInstance::ColorFilmInstance(Device &device, Pipeline &pipeline, const ColorFilm *film) noexcept
: Film::Instance{pipeline, film} {

Kernel1D clear_image_kernel = [](BufferFloat4 image) noexcept {
image.write(dispatch_x(), make_float4(0.f));
};
_clear_image = global_thread_pool().async([&device, clear_image_kernel] {
return device.compile(clear_image_kernel);
});

Kernel1D convert_image_kernel = [this](BufferFloat4 accum, BufferFloat4 output) noexcept {
auto i = dispatch_x();
auto c = accum.read(i);
Expand Down
Loading

0 comments on commit 7d5b7fc

Please sign in to comment.