High-Performance Rendering Framework on Stream Architectures
High-Performance Rendering Framework on Stream Architectures
[Documentation] | [Project Page] | [Paper] | [Discord]
LuisaCompute is a high-performance cross-platform computing framework for graphics and beyond.
LuisaCompute is also the rendering framework described in the SIGGRAPH Asia 2022 paper
LuisaRender: A High-Performance Rendering Framework with Layered and Unified Interfaces on Stream Architectures.
See also LuisaRender for the rendering application as described in the paper.
Welcome to join the discussion channel on Discord!
对于中国大陆的用户,也欢迎加入我们的 QQ 群组:1050189593。
LuisaCompute seeks to balance the seemingly ever-conflicting pursuits for unification, programmability, and performance. To achieve this goal, we design three major components:
To demonstrate the practicality of the system, we also build a Monte Carlo renderer, LuisaRender, atop the framework, which is faster than the state-of-the-art rendering frameworks on modern GPUs.
The DSL in our system provides a unified approach to authoring kernels, i.e., programmable computation tasks on the device. Distinct from typical graphics APIs that use standalone shading languages for device code, our system unifies the authoring of both the host-side logic and device-side kernels into the same language, i.e., modern C++.
The implementation purely relies on the C++ language itself, without any custom preprocessing pass or compiler extension. We exploit meta-programming techniques to simulate the syntax, and function/operator overloading to dynamically trace the user-defined kernels. ASTs are constructed during the tracing as an intermediate representation and later handed over to the backends for generating concrete, platform-dependent shader source code.
Example program in the embedded DSL:
Callable to_srgb = [](Float3 x) {
$if (x = 3.10 required):
```bash
python -m pip install luisa-pythonYou may also build your own wheels with pip:
python -m pip wheel -w
…
bash
git clone -b next https://github.com/LuisaGroup/LuisaCompute.git/ --recursive
```
Since we use Git submodules to manage third-party dependencies, a `--recursive` clone is required.
- Detailed requirements for each platform are listed in [BUILD.md](https://github.com/LuisaGroup/LuisaCompute/blob/stable/BUILD.md).
### Build via the Bootstrap Script
The easiest way to build LuisaCompute is to use the bootstrap script. It can even download and install the required dependencies and build the project.
```bash
python bootstrap.py cmake -f cuda -b # build with CUDA backend using CMake
python bootstrap.py cmake -f cuda -b -- -DCMAKE_BUILD_TYPE=RelWithDebInfo # everything after -- will be passed to CMakeYou may specify -f all to enable all available features on your platform.
To install certain dependencies, you can use the --install or -i option. For example, to install Rust, you can use:
python bootstrap.py -i rustAlternatively, the bootstrap script can output a configuration file for build system without actually building the project. This is useful when you want to use the project inside IDE.
python bootstrap.py cmake -f cuda -c -o cmake-build-release # generate CMake configuration in ./cmake-build-release
…
cpp
#include
// For the DSL sugar macros like $if.
// We exclude this header from to avoid pollution.
// So you have to include it explicitly to use the sugar macros.
#include
using namespace luisa;
using namespace luisa::compute;
int main(int argc, char *argv[]) {
// Step 1.1: Create a context
Context context{argv[0]};
// Step 1.2: Load the CUDA backend plug-in and create a device
Device device = context.create_device("cuda");
// Step 2.1: Create a stream for command submission
Stream stream = device.create_stream();
// Step 2.2: Create an 1024x1024 image with 4-channel 8-bit storage for each pixel; the template
// argument `float` indicates that pixel values reading from or writing to the image
// are converted from `byte4` to `float4` or `float4` to `byte4` automatically
Image device_image = device.create_image(PixelStorage::BYTE4, 1024u, 1024u, 0u);
// Step 3.1: Define kernels to describe the device-side computation
//
// A `Callable` is a function *entity* (not directly inlined during
// the AST recording) that is invocable from kernels or other callables
Callable linear_to_srgb = [](Float4 /* alias for Var */ linear) noexcept {
// The DSL syntax is much like the original C++
auto x = linear.xyz();
return make_float4(
select(1.055f * pow(x, 1.0f / 2.4f) - 0.055f,
12.92f * x,
x */ image) noexcept {
Var coord = dispatch_id().xy();
Var rg = make_float2(coord) / make_float2(dispatch_size().xy());
image->write(coord, linear_to_srgb(make_float4(rg, 1.0f, 1.0f)));
};
// Step 3.2: Compile the kernel into a shader (i.e., a runnable object on the device)
auto fill_image = device.compile(fill_image_kernel);
// Prepare the host memory for holding the image
std::vector download_image(1024u * 1024u * 4u);
// Step 4: Generate commands from resources and shaders, and
// submit them to the stream to execute on the device
stream ; // alignment: 8B
using uint3 = Vector; // alignment: 16B
using uint4 = Vector; // alignment: 16B
// floating-point vectors and matrices
using float2 = Vector; // alignment: 8B
using float3 = Vector; // alignment: 16B
using float4 = Vector; // alignment: 16B
using float2x2 = Matrix; // column-major, alignment: 8B
using float3x3 = Matrix; // column-major, alignment: 16B
using float4x4 = Matrix; // column-major, alignment: 16B⚠️ Please pay attention to the alignment of 3D vectors and matrices --- they are aligned like 4D ones rather than packed. Also, we do not provide 64-bit integer or floating-point vector/matrix types, as they are less useful and typically unsupported on GPUs.
To make vectors/matrices, we provide make_* and read-only swizzle interfaces, e.g.,
auto a = make_float2(); // (0.f, 0.f)
auto b = make_int3(1); // (1, 1, 1)
auto c = make_uint3(b); // (1u, 1u, 1u): converts from a same-dimentional but (possibly) differently typed vector
auto d = make_float3(a, 1.f); // (0.f, 0.f, 1.f): construct float3 from float2 and a float scalar
auto e = d.zzxy(); // (1.f, 1.f, 0.f, 0.f): swizzle
auto m = make_float2x2(1.f); // ((1.f, 0.f,), (0.f, 1.f)): diagonal matrix from a scalar
...Operators are also overloaded for scalar-vector, vector-vector, scalar-matrix, vector-matrix, and matrix-matrix calculations, e.g.,
auto one = make_float2(1.f); // (1.f, 1.f)
auto two = 2.f;
auto three = one + two; // (3.f, 3.f), scalar broadcast to vector
auto m2 = make_float2(2.f); // ((2.f, 0.f), (0.f, 2.f))
auto m3 = 1.5f * m2; // ((3.f, 0.f), (0.f, 3.f)), scalar-matrix multiplication
auto v = m3 * one; // (3.f, 3.f), matrix-vector multiplication, the vector should always
// appear at the right-hand side and is interpreted as a column vector
auto m6 = m2 * m3; // ((6.f, 0.f), (0.f, 6.f)), matrix-matrix multiplicationThe scalar, vector, matrix, and array types are also supported in the DSL, together with make_*, swizzles, and operators. Just wrap them in the Var template or use the pre-defined aliases:
// scalar types; note that 64-bit ones are not supported
using Int = Var;
using UInt = Var;
using Float = Var;
using Bool = Var;
// vector types
using Int2 = Var; // = Var
using Int3 = Var; // = Var
/* ... */
// matrix types
using Float2x2 = Var; // = Var>
using Float3x3 = Var; // = Var>
using Float4x4 = Var; // = Var>
// array types
template
using ArrayVar = Var>;
// make_*
auto a = make_float2(one); // Float2(1.f, 1.f), suppose one = Float(1.f)
auto m = make_float2x2(a, a); // Float2x2((1.f, 1.f), (1.f, 1.f))
auto c = make_int2(a); // Int2(1, 1)
auto d = c.xxx(); // Int3(1, 1, 1)
auto e = d[0]; // 1
/* ... */
// operators
auto v2 = a * 2.f; // Float2(2.f, 2.f)
auto eq = v2 == v2; // Bool2(true, true)
/* ... */
…
cpp
auto a = def(1.f); // equivalent to auto a = def(1.f);
auto b_host = make_float2(1.f); // host C++ variable float2(1.f, 1.f)
auto b_device = def(b_host); // device DSL variable Float2(1.f, 1.f)
/* ... */To export a C++ data struct to the DSL, we provide a helper macro LUISA_STRUCT, which (semi-)automatically reflects the member layouts of the input structure:
// A C++ data structure
namespace foo {
struct alignas(8) S {
float a;
int b;
};
}
// A reflected DSL structure
LUISA_STRUCT(foo::S, a, b) {
/* device-side member functions, e.g., */
[[nodiscard]] auto twice_a() const noexcept { return 2.f * a; }
};
…
cpp
$if (cond) { /*...*/ };
$if (cond) { /*...*/ } $else { /*...*/ };
$if (cond) { /*...*/ } $elif (cond2) { /*...*/ };
$if (cond) { /*...*/ } $elif (cond2) { /*...*/ } $else { /*...*/ };
$while (cond) { /*...*/ };
$for (variable, n) { /*...*/ };
$for (variable, begin, end) { /*...*/ };
$for (variable, begin, end, step) { /*...*/ };
$loop { /*...*/ }; // infinite loop, unless $break'ed
$switch (variable) {
$case (value) { /*...*/ }; // no $break needed inside, as we automatically add one
$default { /*...*/ }; // no $break needed inside, as we automatically add oneNo open issues yet, or sync has not completed.