Remove d3d12 renderer and replace with vulkan
This commit is contained in:
95
build.c
95
build.c
@@ -445,10 +445,14 @@ int main(int argc, char **argv) {
|
||||
////////////////////////////////
|
||||
// Windows build (MSVC cl.exe)
|
||||
|
||||
// Vulkan SDK path — detected from environment or default install location
|
||||
static const char *get_vulkan_sdk_path(void) {
|
||||
const char *env = getenv("VULKAN_SDK");
|
||||
if (env && env[0]) return env;
|
||||
return "C:\\VulkanSDK\\1.4.341.1";
|
||||
}
|
||||
|
||||
static const char *link_libs[] = {
|
||||
"d3d12.lib",
|
||||
"dxgi.lib",
|
||||
"d3dcompiler.lib",
|
||||
"user32.lib",
|
||||
"gdi32.lib",
|
||||
"shell32.lib",
|
||||
@@ -458,6 +462,78 @@ static const char *link_libs[] = {
|
||||
"winmm.lib",
|
||||
};
|
||||
|
||||
////////////////////////////////
|
||||
// SPIR-V shader compilation — compiles .glsl to .spv, then embeds as C header
|
||||
|
||||
static bool compile_shader(const char *glslc_path, const char *src, const char *spv_path, const char *stage) {
|
||||
Nob_Cmd cmd = {0};
|
||||
nob_cmd_append(&cmd, glslc_path, nob_temp_sprintf("-fshader-stage=%s", stage), "-o", spv_path, src);
|
||||
Nob_Cmd_Opt opt = {0};
|
||||
return nob_cmd_run_opt(&cmd, opt);
|
||||
}
|
||||
|
||||
static bool embed_spirv(const char *spv_path, const char *header_path, const char *array_name) {
|
||||
Nob_String_Builder sb = {0};
|
||||
if (!nob_read_entire_file(spv_path, &sb)) return false;
|
||||
|
||||
FILE *out = fopen(header_path, "wb");
|
||||
if (!out) {
|
||||
nob_log(NOB_ERROR, "Could not open %s for writing", header_path);
|
||||
nob_sb_free(sb);
|
||||
return false;
|
||||
}
|
||||
|
||||
fprintf(out, "// Auto-generated from %s — do not edit\n", spv_path);
|
||||
fprintf(out, "#pragma once\n\n");
|
||||
// SPIR-V is U32-aligned, emit as uint32_t array
|
||||
size_t word_count = sb.count / 4;
|
||||
fprintf(out, "#include <stdint.h>\n\n");
|
||||
fprintf(out, "static const uint32_t %s[] = {\n", array_name);
|
||||
const uint32_t *words = (const uint32_t *)sb.items;
|
||||
for (size_t i = 0; i < word_count; i++) {
|
||||
if (i % 8 == 0) fprintf(out, " ");
|
||||
fprintf(out, "0x%08x,", words[i]);
|
||||
if (i % 8 == 7 || i == word_count - 1) fprintf(out, "\n");
|
||||
}
|
||||
fprintf(out, "};\n");
|
||||
|
||||
fclose(out);
|
||||
nob_sb_free(sb);
|
||||
nob_log(NOB_INFO, "Generated %s (%zu bytes)", header_path, sb.count);
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool compile_and_embed_shaders(const char *build_dir) {
|
||||
const char *vk_sdk = get_vulkan_sdk_path();
|
||||
const char *glslc = nob_temp_sprintf("%s\\Bin\\glslc.exe", vk_sdk);
|
||||
|
||||
const char *vert_src = "src/renderer/ui.v.glsl";
|
||||
const char *frag_src = "src/renderer/ui.f.glsl";
|
||||
const char *vert_spv = nob_temp_sprintf("%s/ui_vert.spv", build_dir);
|
||||
const char *frag_spv = nob_temp_sprintf("%s/ui_frag.spv", build_dir);
|
||||
const char *vert_hdr = "src/renderer/ui_vert.spv.h";
|
||||
const char *frag_hdr = "src/renderer/ui_frag.spv.h";
|
||||
|
||||
// Check if rebuild needed
|
||||
if (nob_needs_rebuild1(vert_hdr, vert_src)) {
|
||||
nob_log(NOB_INFO, "Compiling vertex shader");
|
||||
if (!compile_shader(glslc, vert_src, vert_spv, "vertex")) return false;
|
||||
if (!embed_spirv(vert_spv, vert_hdr, "ui_vert_spv")) return false;
|
||||
} else {
|
||||
nob_log(NOB_INFO, "Vertex shader is up to date");
|
||||
}
|
||||
|
||||
if (nob_needs_rebuild1(frag_hdr, frag_src)) {
|
||||
nob_log(NOB_INFO, "Compiling fragment shader");
|
||||
if (!compile_shader(glslc, frag_src, frag_spv, "fragment")) return false;
|
||||
if (!embed_spirv(frag_spv, frag_hdr, "ui_frag_spv")) return false;
|
||||
} else {
|
||||
nob_log(NOB_INFO, "Fragment shader is up to date");
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool build_lunasvg_lib(const char *build_dir, bool debug) {
|
||||
const char *obj_dir = nob_temp_sprintf("%s\\lunasvg_obj", build_dir);
|
||||
const char *lib_path = debug ? "vendor\\lunasvg\\lunasvg_d.lib" : "vendor\\lunasvg\\lunasvg.lib";
|
||||
@@ -626,6 +702,8 @@ int main(int argc, char **argv) {
|
||||
remove("vendor\\freetype\\freetype.lib");
|
||||
remove("vendor\\freetype\\freetype_d.lib");
|
||||
remove("src\\renderer\\font_inter.gen.h");
|
||||
remove("src\\renderer\\ui_vert.spv.h");
|
||||
remove("src\\renderer\\ui_frag.spv.h");
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -639,7 +717,14 @@ int main(int argc, char **argv) {
|
||||
if (!embed_font_file("assets/fonts/Inter-Regular.ttf",
|
||||
"src\\renderer\\font_inter.gen.h")) return 1;
|
||||
|
||||
// Unity build: single cl.exe invocation compiles main.cpp (which #includes everything)
|
||||
// Compile GLSL shaders to SPIR-V and embed as C headers
|
||||
if (!compile_and_embed_shaders(build_dir)) return 1;
|
||||
|
||||
const char *vk_sdk = get_vulkan_sdk_path();
|
||||
const char *vk_include = nob_temp_sprintf("/I%s/Include", vk_sdk);
|
||||
const char *vk_lib = nob_temp_sprintf("%s/Lib/vulkan-1.lib", vk_sdk);
|
||||
|
||||
// Unity build: single cl.exe invocation compiles main.cpp (which #includes everything including the vulkan renderer)
|
||||
{
|
||||
Nob_Cmd cmd = {0};
|
||||
nob_cmd_append(&cmd, "cl.exe");
|
||||
@@ -647,6 +732,7 @@ int main(int argc, char **argv) {
|
||||
nob_cmd_append(&cmd, "/Isrc", "/Ivendor/clay");
|
||||
nob_cmd_append(&cmd, "/Ivendor/lunasvg/include");
|
||||
nob_cmd_append(&cmd, "/Ivendor/freetype/include");
|
||||
nob_cmd_append(&cmd, vk_include);
|
||||
nob_cmd_append(&cmd, "/DLUNASVG_BUILD_STATIC");
|
||||
|
||||
if (debug) {
|
||||
@@ -668,6 +754,7 @@ int main(int argc, char **argv) {
|
||||
nob_cmd_append(&cmd, "/DEBUG");
|
||||
nob_cmd_append(&cmd, debug ? "vendor/lunasvg/lunasvg_d.lib" : "vendor/lunasvg/lunasvg.lib");
|
||||
nob_cmd_append(&cmd, debug ? "vendor/freetype/freetype_d.lib" : "vendor/freetype/freetype.lib");
|
||||
nob_cmd_append(&cmd, vk_lib);
|
||||
{
|
||||
size_t i;
|
||||
for (i = 0; i < NOB_ARRAY_LEN(link_libs); i++)
|
||||
|
||||
Reference in New Issue
Block a user