Skip to content

Commit 90250c1

Browse files
committed
Load shaders from executable-relative path
1 parent 8007904 commit 90250c1

5 files changed

Lines changed: 50 additions & 19 deletions

File tree

CMakeLists.txt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,5 @@ add_executable(vk-bench src/main.cpp)
3535
add_dependencies(vk-bench vk-bench-shaders)
3636
target_include_directories(vk-bench PRIVATE include)
3737
target_link_libraries(vk-bench PRIVATE Vulkan::Vulkan)
38-
target_compile_definitions(vk-bench PRIVATE VK_BENCH_SHADER_DIR="/usr/local/share/vk-bench/shaders")
39-
4038
install(TARGETS vk-bench RUNTIME DESTINATION bin)
41-
install(FILES ${SHADER_OUTPUTS} DESTINATION share/vk-bench/shaders)
39+
install(FILES ${SHADER_OUTPUTS} DESTINATION bin/shaders)

Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ COPY shaders /workspace/shaders
2323
RUN cmake -S /workspace -B /workspace/build -DCMAKE_BUILD_TYPE=Release \
2424
&& cmake --build /workspace/build --config Release -j"$(nproc)" \
2525
&& install -m 0755 /workspace/build/vk-bench /usr/local/bin/vk-bench \
26-
&& install -d /usr/local/share/vk-bench/shaders \
27-
&& cp /workspace/build/shaders/*.spv /usr/local/share/vk-bench/shaders/
26+
&& install -d /usr/local/bin/shaders \
27+
&& cp /workspace/build/shaders/*.spv /usr/local/bin/shaders/
2828

2929
COPY docker/entrypoint.sh /entrypoint.sh
3030
ENTRYPOINT ["/entrypoint.sh"]

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ This Level 0 repo intentionally scopes to **one executable** (`vk-bench`) and **
1414

1515
No assets, textures, or engine features are included.
1616

17-
Shaders are authored in **Slang** and compiled to SPIR-V during build.
17+
Shaders are authored in **Slang** and compiled to SPIR-V during build, then installed next to the executable (`<bin>/shaders`) for cross-platform runtime lookup.
1818

1919
## How to run
2020

docker/Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ COPY shaders /workspace/shaders
2323
RUN cmake -S /workspace -B /workspace/build -DCMAKE_BUILD_TYPE=Release \
2424
&& cmake --build /workspace/build --config Release -j"$(nproc)" \
2525
&& install -m 0755 /workspace/build/vk-bench /usr/local/bin/vk-bench \
26-
&& install -d /usr/local/share/vk-bench/shaders \
27-
&& cp /workspace/build/shaders/*.spv /usr/local/share/vk-bench/shaders/
26+
&& install -d /usr/local/bin/shaders \
27+
&& cp /workspace/build/shaders/*.spv /usr/local/bin/shaders/
2828

2929
COPY docker/entrypoint.sh /entrypoint.sh
3030
ENTRYPOINT ["/entrypoint.sh"]

src/main.cpp

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
#include <chrono>
55
#include <cstdint>
66
#include <cstdlib>
7+
#include <cstring>
8+
#include <filesystem>
79
#include <fstream>
810
#include <iomanip>
911
#include <iostream>
@@ -13,10 +15,6 @@
1315
#include <string>
1416
#include <vector>
1517

16-
#ifndef VK_BENCH_SHADER_DIR
17-
#define VK_BENCH_SHADER_DIR "./shaders"
18-
#endif
19-
2018
struct Config {
2119
bool headless = false;
2220
uint32_t frames = 300;
@@ -108,6 +106,41 @@ Config parse_args(int argc, char **argv) {
108106
return cfg;
109107
}
110108

109+
std::string pick_shader_dir(const char *argv0) {
110+
namespace fs = std::filesystem;
111+
112+
std::vector<fs::path> candidates;
113+
if (argv0 != nullptr && std::strlen(argv0) > 0) {
114+
fs::path exe_path(argv0);
115+
if (!exe_path.is_absolute()) {
116+
exe_path = fs::absolute(exe_path);
117+
}
118+
candidates.push_back(exe_path.parent_path() / "shaders");
119+
}
120+
121+
#ifdef VK_BENCH_SHADER_DIR
122+
candidates.emplace_back(VK_BENCH_SHADER_DIR);
123+
#endif
124+
candidates.emplace_back(fs::current_path() / "shaders");
125+
126+
for (const auto &candidate : candidates) {
127+
std::error_code ec;
128+
if (fs::exists(candidate / "triangle.vert.spv", ec) &&
129+
fs::exists(candidate / "triangle.frag.spv", ec)) {
130+
return candidate.string();
131+
}
132+
}
133+
134+
std::string searched;
135+
for (const auto &candidate : candidates) {
136+
if (!searched.empty()) {
137+
searched += ", ";
138+
}
139+
searched += candidate.string();
140+
}
141+
fail("Failed to locate shader directory. Checked: " + searched);
142+
}
143+
111144
std::vector<uint32_t> read_spirv(const std::string &path) {
112145
std::ifstream file(path, std::ios::binary | std::ios::ate);
113146
if (!file) {
@@ -179,7 +212,8 @@ void destroy_buffer(VkDevice device, BufferWithMemory &buffer) {
179212

180213
TriangleResources create_triangle_resources(VkPhysicalDevice physical,
181214
VkDevice device, uint32_t width,
182-
uint32_t height) {
215+
uint32_t height,
216+
const std::string &shader_dir) {
183217
TriangleResources out{};
184218

185219
VkImageCreateInfo ici{VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO};
@@ -267,10 +301,8 @@ TriangleResources create_triangle_resources(VkPhysicalDevice physical,
267301
fail("vkCreateFramebuffer failed");
268302
}
269303

270-
const auto vert =
271-
read_spirv(std::string(VK_BENCH_SHADER_DIR) + "/triangle.vert.spv");
272-
const auto frag =
273-
read_spirv(std::string(VK_BENCH_SHADER_DIR) + "/triangle.frag.spv");
304+
const auto vert = read_spirv(shader_dir + "/triangle.vert.spv");
305+
const auto frag = read_spirv(shader_dir + "/triangle.frag.spv");
274306

275307
VkShaderModuleCreateInfo smci{VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO};
276308
smci.codeSize = vert.size() * sizeof(uint32_t);
@@ -482,10 +514,11 @@ int main(int argc, char **argv) {
482514

483515
const bool graphics_scene =
484516
cfg.scene == "triangle" || cfg.scene == "million-tris";
517+
const std::string shader_dir = pick_shader_dir(argv[0]);
485518
TriangleResources triangle{};
486519
if (graphics_scene) {
487-
triangle =
488-
create_triangle_resources(physical, device, cfg.width, cfg.height);
520+
triangle = create_triangle_resources(physical, device, cfg.width,
521+
cfg.height, shader_dir);
489522
}
490523

491524
BufferWithMemory src{};

0 commit comments

Comments
 (0)