Skip to content
Open
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
37 changes: 28 additions & 9 deletions defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ _shader_file_extensions = [
'.comp',
'.glsl',
'.spvasm',
'.h',
]

_shader_stages = [
Expand All @@ -18,29 +19,46 @@ _shader_stages = [
'compute',
]

def _SpvOutput(srcs):
return [src.basename + '.spv' for src in srcs]

def _SpvasmOutput(srcs):
return [src.basename + '.spvasm' for src in srcs]
def _gather_incdirs(files):
# Use a dict to avoid duplicates (sets are not allowed in Starlark)
incdirs_dict = {}
for f in files:
path = f.path
if "/" in path:
parent_dir = path.rsplit("/", 1)[0]
incdirs_dict[parent_dir] = True
incdirs_list = []
for d in sorted(incdirs_dict.keys()):
incdirs_list.append("-I" + d)
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

I don't think having every source file parent path as an include directory for every file is very expected, no?

Does glslc not already find headers relative to the source files?

return incdirs_list

def _shader_binary(ctx):
spvOut = ctx.actions.declare_file(ctx.attr.name + ".spv")
shaderStage = ''

all_inputs = list(ctx.files.srcs)
incArgs = _gather_incdirs(all_inputs)

glslcArgs = []

if ctx.attr.stage:
glslcArgs.append("-shader-stage=" + ctx.attr.stage)

for src in ctx.files.srcs:
glslcArgs.append(src.path)
# Add include paths and user options
glslcArgs += incArgs
glslcArgs += ctx.attr.opts

# Add sources (excluding headers)
compile_inputs = []
for f in all_inputs:
if not f.path.endswith(".h"):
glslcArgs.append(f.path)
compile_inputs.append(f)

glslcArgs.append('-o')
glslcArgs.append(spvOut.path)

ctx.actions.run(
inputs = ctx.files.srcs,
inputs = all_inputs,
outputs = [spvOut],
executable = ctx.executable.glslc,
arguments = glslcArgs,
Expand All @@ -62,6 +80,7 @@ shader_binary = rule(
default = '',
values = [''] + _shader_stages,
),
"opts": attr.string_list(default = []),
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

instead of a general opts could we emulate what the cc_* libraries do and have a defines attribute for defines etc? https://bazel.build/reference/be/c-cpp#cc_library.defines

"glslc": attr.label(
default = Label("@vulkan_sdk//:glslc"),
allow_single_file = True,
Expand Down