hlsl
Skill1.1k starsChanged 35 days agoApache-2.0View on GitHub
HLSL code generation, StringBuilder patterns, builtin headers, and DXIL embedding.
Trending
Quiet since July.thin dataabout the whole repository
Who's talking about LuisaGroup/LuisaCompute
- Stack Overflow“c++ error: ‘struct std::atomic_flag’ has no member named ‘test’”Stack Overflow · Dec 26, 2022 · 1 votes
- Tencent/ncnn“optimize multi-head attention (increased shared-memory tile size, add fused kernel)”GitHub · Jul 10, 2026
- bazelbuild/bazel-central-registry“wanted: [LuisaGroup/LuisaCompute]”GitHub · Nov 8, 2025
- MuGdxy/SymEigen“Try to add support for luisa-compute”GitHub · Sep 15, 2025
- LuisaGroup/LuisaRender“Remove `util/u64.h` and use `Var<ulong>` instead”GitHub · Jun 2, 2024
The biggest 5 of 5 mentions linking to the repository that our crawl found. Each line is their own headline.
In this repository22 other files it gives its agents.
- .github/copilot-instructions.mdCopilot instructions
- astSkill
- backend-architectureSkill
- cmakeSkill
- cpp-styleSkill
- debugSkill
- gitSkill
- glslangSkill
- ir-pipelineSkill
- lc_apiSkill
- lc_native_shaderSkill
- lc_optimizeSkill
- llvm-spirvSkill
- lspSkill
- project_structureSkill
- renderdocSkill
- spirv_codegenSkill
- spv-optSkill
- testSkill
- xir-passesSkill
- xmakeSkill
- yyjsonSkill
The file
We split this skill into the rules it gives an agent
3 rules. About 0 are common, 0 are distinctive, and 3 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈0
Distinctive · estimate
This author's own choices. Worth a look.
3
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 148
Use "text"sv literals to avoid allocations
- Very specific: line 152
Check size() before modifying last character
- Very specific: line 227
Add .bytes or .dxil to src/backends/common/hlsl/builtin/ - e.g.
≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
HLSL Code Generation
Codegen utilities using vstd::StringBuilder and formatting helpers.
StringBuilder
#include <luisa/vstl/string_builder.h>
vstd::StringBuilder builder;
builder.clear();
size_t sz = builder.size();
vstd::string_view view = builder.view();
char* data = builder.data();
char& ch = builder[0]; // operator[] (both const and non-const)
builder.reserve(1024);
builder.resize(64);Appending
str.append("Hello");
str += "World"sv;
str.append(' ');
str << '\n';
str.append(other_builder);
str << "func" << '(' << ")";Numbers
vstd::to_string(42, str); // int (faster than format)
vstd::to_string(3.14f, str); // float
vstd::string s = vstd::to_string(42); // single-arg form -> vstd::string
str << luisa::format("{}", 42);
str << luisa::format("{}, {}!", "Hello", "World");
str << luisa::format("{:016X}", hash); // same as luisa::hash_to_string(hash)String View Literals
str += "void"sv; // no allocation<luisa/vstl/common.h> already enables the literals: it includes vstl/vstring.h, which
includes vstl/string_hash.h - and that header has using namespace std::literals; at
(global) namespace scope. So generator .cpp files write "..."sv without their own
using namespace std::string_view_literals;.
Code Generation Patterns
Function Declaration
// real source: CodegenUtility members, codegen_utils/function_codegen.cpp
void GetFunctionDecl(Function func, vstd::StringBuilder &str) {
vstd::StringBuilder data;
if (func.return_type()) CodegenUtility::GetTypeName(*func.return_type(), data, Usage::READ);
else data += "void"sv;
data += " "sv;
CodegenUtility::GetFunctionName(func, data);
data += '(';
for (auto &&arg : func.arguments()) {
Usage usage = func.variable_usage(arg.uid());
CodegenUtility::GetTypeName(*arg.type(), data, usage);
data << ' ';
vstd::StringBuilder varName;
CodegenUtility::GetVariableName(func, arg, varName);
data << varName << ',';
}
if (!func.arguments().empty()) data[data.size() - 1] = ')';
else data += ')';
str << '\n' << data;
}Template Parameters
str << "template<"sv;
for (uint64 i = 0; i < tempIdx; ++i) {
str << "typename T"sv; vstd::to_string(static_cast<int64_t>(i), str); str << ',';
}
if (tempIdx > 0) *(str.end() - 1) = '>';Call Expression
str << "func_name"sv << '(';
if (!args.empty()) {
for (size_t i = 0; i < args.size() - 1; ++i) { args[i]->accept(vis); str << ','; }
args.back()->accept(vis);
}
str << ')';Type-Aware Generation
// buffer / texture arguments become template parameters T0, T1, ... (GetFunctionDecl)
if (t->is_texture() || t->is_buffer()) { str << 'T'; vstd::to_string(tempIdx++, str); }
// GetTypeName: element type name followed by the dimension
else if (t->is_vector()) { GetTypeName(*t->element(), str, usage); vstd::to_string(t->dimension(), str); }
// a 3-row matrix is stored as float3x4 (columns are padded to 4)
else if (t->is_matrix()) { GetTypeName(*t->element(), str, usage); vstd::to_string(t->dimension(), str); str << 'x'; vstd::to_string(t->dimension() == 3 ? 4 : t->dimension(), str); }
// matrix element of a buffer/texture is wrapped: "_WrappedFloat" + n + 'x' + n,
// with n = vstd::to_string(t->element()->dimension()) (type_system.cpp, BUFFER case)RAII Wrapping
// StringStateVisitor::Scope - the real RAII wrapper of the generator
struct Scope {
StringStateVisitor *self;
explicit Scope(StringStateVisitor *self); // self->str << "{\n"sv
~Scope() { self->str << "}\n"sv; }
};
Scope scope{this}; // braces emitted automatically
// non-RAII case (balanced open/close emitted by hand, e.g. aliased struct casts):
AliasedToOrigin(expr->type(), str);
str << '(';
// ... inner content ...
str << ')';Trailing Comma Fix
str << '(';
for (auto &&item : items) str << item << ',';
if (!items.empty()) str[str.size() - 1] = ')';
else str << ')';Iteration & Hash
for (char c : str) {}
str.erase(str.begin() + 5);
vstd::hash<vstd::StringBuilder> hasher; size_t h = hasher(builder);
if (builder1 == builder2) {}
if (builder1 == "literal"sv) {}Best Practices
- Use
"text"svliterals to avoid allocations vstd::to_string()for integers (faster thanluisa::format())builder.reserve(1024)when size knownoperator<<for chaining,operator+=for single items- Check
size()before modifying last character
HLSL Builtins
Located in src/backends/common/hlsl/builtin/. Access via:
// included as "../builtin/hlsl_builtin.hpp" by codegen_utils/entry_points.cpp,
// under `#ifndef LC_NO_HLSL_BUILTIN`
auto header = lc_hlsl::get_hlsl_builtin("hlsl_header"); // {void const* ptr; size_t size;}
std::string_view code(static_cast<const char*>(header.ptr), header.size);Generator and backend code never calls get_hlsl_builtin directly - it goes through
CodegenUtility::ReadInternalHLSLFile(name) (codegen_utils/entry_points.cpp), which
returns the same bytes as a vstd::string_view. The DX/VK shader loaders also use it for
pre-compiled blobs: CacheType::Internal (src/backends/dx/Shader/ComputeShader.cpp)
and SerdeType::kBuiltin (src/backends/vk/shader_serializer.cpp) try the embedded
asset first, then fall back to BinaryIO::read_internal_shader.
With LC_NO_HLSL_BUILTIN defined (CMake LUISA_COMPUTE_VULKAN_ENABLE_DXC_COMPATIBILITY=OFF,
xmake: lc_vk_backend_enable_dxc_compatibility not set) get_hlsl_builtin is stubbed out and
returns {}, so no builtin is available.
Header Files (.bytes)
Keys are the strings passed to ReadInternalHLSLFile / get_hlsl_builtin. The backing file is builtin/<key> with .bytes appended unless the key already carries the extension (three keys below keep .bytes in the lookup string), and the BC6/BC7 encoder fragments are extensionless files.
| Key | Description |
hlsl_header | Main HLSL header |
hlsl_header_fallback | Fallback header (noRegister path, e.g. DX without DXC) |
spv_alias | SPIR-V aliases (SPIR-V route only) |
fallback_rtx_header | Software ray-tracing traversal (DeviceConfigExt::use_fallback_rtx()) |
dx_linalg | Linear algebra utils (DXIL) |
vk_linalg | Linear algebra utils (SPIR-V) |
raytracing_header | Ray tracing |
raytracing_motion_header | Ray tracing motion blur |
tex2d_bindless / tex3d_bindless | Bindless textures |
compute_quad | Compute quad ops |
determinant / inverse | Matrix ops |
indirect | Indirect dispatch/draw |
resource_size | Resource queries |
accel_header | Acceleration structures |
copy_sign | Sign ops |
bindless_common | Bindless utils |
auto_diff | Autodiff |
reduce | Parallel reduction |
bindless_upload.bytes | Bindless upload source -> compiled to load_bdls.dxil |
accel_process.bytes | Acceleration-structure processing source -> set_accel4.dxil |
accel_process_vk_motion.bytes | Acceleration-structure processing with motion blur |
bc6_header / bc6_encode_block / bc6_trymode_g10cs / bc6_trymode_le10cs | BC6 encoder HLSL fragments |
bc7_header / bc7_encode_block / bc7_trymode_02cs / bc7_trymode_137cs / bc7_trymode_456cs | BC7 encoder HLSL fragments |
builtin/bindless_upload, builtin/bindless_upload_vk and builtin/load_bdls_dxil are
extensionless files that are not embedded (neither the CMake list nor the xmake globs
match them) and have no dict key - the registered sources are bindless_upload.bytes and
load_bdls.dxil.
DXIL Files (.dxil)
Pre-compiled builtin shader artifacts (ShaderSerializer containers), keyed with their full file name.
| Key | Description |
accel_process_vk.dxil | Vulkan acceleration structures |
load_bdls.dxil / load_bdls_vk.dxil | Bindless loading |
set_accel4.dxil | Set acceleration structure |
bc6_encodeblock.dxil / bc6_trymodeg10.dxil / bc6_trymodele10.dxil | BC6 compression |
bc7_encodeblock.dxil / bc7_trymode02.dxil / bc7_trymode137.dxil / bc7_trymode456.dxil | BC7 compression |
*_vk.dxil are legacy HLSL-to-SPIR-V (dxc -spirv) containers: the current Vulkan
builtin kernels load .spv artifacts embedded as luisa_compute_vk_builtin_*
(src/backends/vk/builtin_kernel.cpp), and scripts/verify_compile_builtin.py treats
load_bdls_vk.dxil as a stale v2 container.
Adding Builtins
- Add
.bytesor.dxiltosrc/backends/common/hlsl/builtin/- e.g.my_helper.bytes. A.bytesasset is hand-written HLSL text that is edited in place (nothing generates it from a.hlsl). - Declare the symbol
luisa_embed_device_lib/bin2objproduces for that file (the file stem with its extension turned into_, because of--preserve-ext), insideextern "C" { ... }inbuiltin/hlsl_builtin.hpp:LC_HLSL_DECL_VARNAME(my_helper_bytes) - Register the lookup key:
LC_HLSL_INSERT_VARNAME(my_helper_bytes, "my_helper") - CMake: append
my_helper.bytestoLUISA_COMPUTE_HLSL_DEVICE_LIB_SOURCESinsrc/backends/common/hlsl/CMakeLists.txt- the list is explicit, new files are not globbed. - xmake: nothing to do for
.bytes/.dxil(src/backends/common/hlsl/xmake.luaglobsbuiltin/*.bytesandbuiltin/*.dxil); extensionless assets need their ownadd_files(...)entry with{rules = "utils.bin2obj"}.
Embedding: xmake uses the utils.bin2obj rule plus add_defines('LUISA_BIN_2_OBJ'), so the
_binary_<name>_start/_end symbols are produced per object file. CMake runs the host tool
luisa_embed_device_lib (utils/embed_device_lib.cpp) with --unsigned ... --preserve-ext
in the luisa-compute-backend-hlsl-embed target to regenerate
builtin/hlsl_builtin_embed.cpp (gitignored). That target runs the tool twice - once to
write, once with --check - and luisa-compute-backend-hlsl-embed-check is the standalone
check-only variant; luisa-compute-hlsl-codegen depends on the embed target.
--preserve-ext is why hlsl_header.bytes becomes the symbol hlsl_header_bytes.
Regenerating a compiled .dxil builtin from its .bytes source (same compiler as the
runtime, src/backends/tools/main.cpp):
xmake build lc_compile_builtin
xmake run lc_compile_builtin dx src/backends/common/hlsl/builtin/bindless_upload.bytes \
src/backends/common/hlsl/builtin/load_bdls.dxil --name load_bdls.dxil --verify
xmake run lc_compile_builtin vk <input-shader> <output> # dxc -spirv, VK v10 container
xmake run lc_compile_builtin dx|vk inspect <artifact> # decode contract fields--shader-model <n> is a packed model (62 == cs_6_2, default 62); DX runtime
selection is kShaderModel = 65, kHighShaderModel = 66 (allowed warp size),
kTensorShaderModel = 69 (cooperative ops / 8-bit, src/backends/dx/DXApi/LCDevice.cpp).
Dumped HLSL can also be compiled by hand: python scripts/compile_dxil.py [source]
[--shader-model 6_5|6_6|6_9] [-E main] [--dxc <dxc.exe>] [-o out.dxil] (dxc is
auto-searched: D:\DirectXShaderCompiler\build\bin\dxc.exe, then PATH, then
bin/debug/dxc.exe). scripts/compile_builtin.py dx|vk <output> [--name <kernel>] [--no-build]
is a thin wrapper that first builds lc_compile_builtin in release.
Codegen Debug
Set env LUISA_DUMP_SOURCE=1 to dump generated HLSL to per-shader files.
Output: hlsl_output_<shader_name>.hlsl in the working directory.
Naming priority depends on the backend and shader path:
DX compute / raster / save paths:
ShaderOption::name/fileName— user-provided nameFunction::name()— kernel/callable debug nameFunction::hash()formatted as hex — fallback (e.g.hlsl_output_a1b2c3d4.hlsl)
VK compute on the HLSL-to-SPIR-V compatibility route (ComputeShader::compile, src/backends/vk/compute_shader.cpp):
file_name—ShaderOption::name, else the cache name<md5>.spvwhenenable_cache- MD5 of the generated HLSL — fallback
VK ray tracing (RayTracingShader::compile, src/backends/vk/rt_shader.cpp):
file_name → code_md5 → literal rt_unknown.
VK raster (VkRasterExt):
ShaderOption::name is always required (LUISA_ASSERT(!option.name.empty(), "Raster shader name must not be empty.")), so the dump file uses that name.
VK user compute normally goes through native XIR-to-SPIR-V; it falls back to this HLSL route only for features the native codegen cannot express - native_include, device printing, async_copy/pipeline commit+wait, motion blur, or software (fallback-RTX) ray tracing (lc::vk::detail::plan_user_compute_codegen_route in src/backends/vk/user_compute_codegen_route.h). LUISA_VULKAN_REQUIRE_NATIVE_XIR_SPIRV=1 forbids the HLSL fallback, and LC_NO_HLSL_BUILTIN (no DXC compatibility) removes the HLSL route entirely - then those features must be compiled by LUISA_XIR_TO_SPIRV or LUISA_AST_LLVM_TO_SPIRV.
Per-shader dumps are written with "wb" (overwrite). The one exception is the VK option.compile_only path (src/backends/vk/device.cpp), which still appends everything to a single hlsl_output.hlsl with "ab".
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

