Actually fix RenderDoc issue

Factor out shader compilation stage GLSL -> SPIRV
This commit is contained in:
David Gonzalez Martin 2024-12-27 19:18:17 -06:00 committed by David
parent a376fba285
commit b6220d182c
9 changed files with 18 additions and 194 deletions

View File

@ -211,7 +211,7 @@ if (NOT BB_IS_CI)
set(VOLK_STATIC_DEFINES VK_USE_PLATFORM_WIN32_KHR)
target_compile_definitions(${COMPILER_NAME} PRIVATE GLFW_EXPOSE_NATIVE_WIN32)
elseif(CMAKE_SYSTEM_NAME STREQUAL Linux)
set(VOLK_STATIC_DEFINES VK_USE_PLATFORM_XLIB_KHR)
set(VOLK_STATIC_DEFINES VK_USE_PLATFORM_XCB_KHR)
target_compile_definitions(${COMPILER_NAME} PRIVATE GLFW_EXPOSE_NATIVE_X11)
elseif(CMAKE_SYSTEM_NAME STREQUAL Darwin)
set(VOLK_STATIC_DEFINES VK_USE_PLATFORM_METAL_EXT)
@ -237,7 +237,6 @@ if (NOT BB_IS_CI)
target_sources(${COMPILER_NAME} PRIVATE
"bootstrap/std/image_loader.c"
"bootstrap/std/shader_compilation.c"
"bootstrap/std/font_cache.c"
"bootstrap/std/font_provider.c"
"bootstrap/std/window.c"

View File

@ -4,7 +4,6 @@
#include <std/virtual_buffer.h>
#include <std/window.h>
#include <std/render.h>
#include <std/shader_compilation.h>
#include <std/image_loader.h>
#include <std/font_provider.h>
#include <std/ui_core.h>

View File

@ -114,7 +114,7 @@ declare_slice(PipelineLayoutCreate);
STRUCT(GraphicsPipelinesCreate)
{
Slice(String) shader_sources;
Slice(String) shader_binaries;
Slice(PipelineLayoutCreate) layouts;
Slice(PipelineCreate) pipelines;
};

View File

@ -1,8 +0,0 @@
#pragma once
#include <std/base.h>
#include <std/os.h>
#include <std/render.h>
EXPORT String compile_shader(Arena* arena, String path, ShaderStage shader_stage);

View File

@ -1,5 +1,4 @@
#include <std/render.h>
#include <std/shader_compilation.h>
#include <std/string.h>
#include <std/image_loader.h>
#include <std/virtual_buffer.h>
@ -325,14 +324,14 @@ void buffer_copy_to_host(VulkanBuffer buffer, Slice(HostBufferCopy) regions)
}
}
fn VkShaderStageFlags vulkan_shader_stage_from_path(String shader_source_path)
fn VkShaderStageFlags vulkan_shader_stage_from_path(String shader_binary_path)
{
VkShaderStageFlags shader_stage;
if (string_ends_with(shader_source_path, strlit(".vert")))
if (string_ends_with(shader_binary_path, strlit(".vert.spv")))
{
shader_stage = VK_SHADER_STAGE_VERTEX_BIT;
}
else if (string_ends_with(shader_source_path, strlit(".frag")))
else if (string_ends_with(shader_binary_path, strlit(".frag.spv")))
{
shader_stage = VK_SHADER_STAGE_FRAGMENT_BIT;
}
@ -1098,9 +1097,9 @@ Renderer* renderer_initialize(Arena* arena)
vkok(vkCreateSampler(renderer->device, &create_info, renderer->allocator, &renderer->sampler));
}
String shader_source_paths[] = {
strlit("bootstrap/std/shaders/rect.vert"),
strlit("bootstrap/std/shaders/rect.frag"),
String shader_binaries[] = {
strlit(BUILD_DIR "/rect.vert.spv"),
strlit(BUILD_DIR "/rect.frag.spv"),
};
PipelineLayoutCreate pipeline_layouts[] = {
@ -1135,14 +1134,14 @@ Renderer* renderer_initialize(Arena* arena)
auto create_data = (GraphicsPipelinesCreate) {
.layouts = array_to_slice(pipeline_layouts),
.pipelines = array_to_slice(pipeline_create),
.shader_sources = array_to_slice(shader_source_paths),
.shader_binaries = array_to_slice(shader_binaries),
};
auto graphics_pipeline_count = create_data.pipelines.length;
assert(graphics_pipeline_count);
auto pipeline_layout_count = create_data.layouts.length;
assert(pipeline_layout_count);
assert(pipeline_layout_count <= graphics_pipeline_count);
auto shader_count = create_data.shader_sources.length;
auto shader_count = create_data.shader_binaries.length;
VkPipeline pipeline_handles[BB_PIPELINE_COUNT];
VkPipelineShaderStageCreateInfo shader_create_infos[MAX_SHADER_MODULE_COUNT_PER_PIPELINE];
@ -1270,11 +1269,9 @@ Renderer* renderer_initialize(Arena* arena)
for (u64 i = 0; i < shader_count; i += 1)
{
String shader_source_path = create_data.shader_sources.pointer[i];
String shader_binary_path = create_data.shader_binaries.pointer[i];
ShaderStage shader_stage = shader_stage_from_path(shader_source_path);
auto binary = compile_shader(arena, shader_source_path, shader_stage);
auto binary = file_read(arena, shader_binary_path);
VkShaderModuleCreateInfo create_info = {
.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO,
@ -1376,7 +1373,7 @@ Renderer* renderer_initialize(Arena* arena)
for (u64 i = 0; i < pipeline_shader_count; i += 1)
{
auto shader_index = create.shader_source_indices.pointer[i];
auto shader_source_path = create_data.shader_sources.pointer[shader_index];
auto shader_source_path = create_data.shader_binaries.pointer[shader_index];
shader_create_infos[i] = (VkPipelineShaderStageCreateInfo) {
.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,

View File

@ -1,136 +0,0 @@
#include <std/shader_compilation.h>
#if SHADER_COMPILATION_USE_SOURCE
#include <glslang/Include/glslang_c_interface.h>
// Required for use of glslang_default_resource
#include <glslang/Public/resource_limits_c.h>
typedef struct SpirVBinary {
uint32_t *words; // SPIR-V words
int size; // number of words in SPIR-V binary
} SpirVBinary;
fn SpirVBinary compileShaderToSPIRV_Vulkan(Arena* arena, glslang_stage_t stage, String shader_source, String filename)
{
if (!glslang_initialize_process())
{
failed_execution();
}
const glslang_input_t input = {
.language = GLSLANG_SOURCE_GLSL,
.stage = stage,
.client = GLSLANG_CLIENT_VULKAN,
.client_version = GLSLANG_TARGET_VULKAN_1_3,
.target_language = GLSLANG_TARGET_SPV,
.target_language_version = GLSLANG_TARGET_SPV_1_6,
.code = string_to_c(shader_source),
.default_version = 450,
.default_profile = GLSLANG_NO_PROFILE,
.force_default_version_and_profile = false,
.forward_compatible = false,
.messages = GLSLANG_MSG_DEFAULT_BIT,
.resource = glslang_default_resource(),
};
glslang_shader_t* shader = glslang_shader_create(&input);
SpirVBinary bin = {
.words = NULL,
.size = 0,
};
if (!glslang_shader_preprocess(shader, &input)) {
print("GLSL preprocessing failed {s}\n", filename);
print("{cstr}\n", glslang_shader_get_info_log(shader));
print("{cstr}\n", glslang_shader_get_info_debug_log(shader));
print("{cstr}\n", input.code);
glslang_shader_delete(shader);
return bin;
}
if (!glslang_shader_parse(shader, &input)) {
print("GLSL parsing failed {s}\n", filename);
print("{cstr}\n", glslang_shader_get_info_log(shader));
print("{cstr}\n", glslang_shader_get_info_debug_log(shader));
print("{cstr}\n", glslang_shader_get_preprocessed_code(shader));
glslang_shader_delete(shader);
return bin;
}
glslang_program_t* program = glslang_program_create();
glslang_program_add_shader(program, shader);
if (!glslang_program_link(program, GLSLANG_MSG_SPV_RULES_BIT | GLSLANG_MSG_VULKAN_RULES_BIT)) {
print("GLSL linking failed {s}\n", filename);
print("{cstr}\n", glslang_program_get_info_log(program));
print("{cstr}\n", glslang_program_get_info_debug_log(program));
glslang_program_delete(program);
glslang_shader_delete(shader);
return bin;
}
glslang_program_SPIRV_generate(program, stage);
bin.size = glslang_program_SPIRV_get_size(program);
bin.words = arena_allocate(arena, u32, bin.size);
glslang_program_SPIRV_get(program, bin.words);
const char* spirv_messages = glslang_program_SPIRV_get_messages(program);
if (spirv_messages)
print("({s}) {cstr}\b", filename, spirv_messages);
glslang_program_delete(program);
glslang_shader_delete(shader);
return bin;
}
#endif
String compile_shader(Arena* arena, String path, ShaderStage shader_stage)
{
#if SHADER_COMPILATION_USE_SOURCE
auto file = file_read(arena, path);
glslang_stage_t stage;
switch (shader_stage)
{
case SHADER_STAGE_VERTEX:
stage = GLSLANG_STAGE_VERTEX;
break;
case SHADER_STAGE_FRAGMENT:
stage = GLSLANG_STAGE_FRAGMENT;
break;
}
auto result = compileShaderToSPIRV_Vulkan(arena, stage, file, path);
return (String) {
.pointer = (u8*)result.words,
.length = result.size * sizeof(*result.words),
};
#else
unused(shader_stage);
auto output_path = arena_join_string(arena, (Slice(String))array_to_slice(((String[]) {
path,
strlit(".spv"),
})));
char* arguments[] = {
#if _WIN32
"C:/VulkanSDK/1.3.296.0/Bin/glslangValidator.exe",
#elif defined (__APPLE__)
"/opt/homebrew/bin/glslangValidator",
#else
"/usr/bin/glslangValidator",
#endif
"-V",
string_to_c(path),
"-o",
string_to_c(output_path),
0,
};
run_command(arena, (CStringSlice)array_to_slice(arguments), 0);
auto file = file_read(arena, output_path);
return file;
#endif
}

View File

@ -1 +0,0 @@
*.spv

View File

@ -11,8 +11,6 @@ global_variable OSWindowingCallbacks callbacks;
// TODO: thread local
global_variable OSEventQueue* event_queue = 0;
global_variable u8 use_x11 = 0;
fn void monitor_callback(GLFWmonitor* monitor, int event)
{
unused(monitor);
@ -45,13 +43,8 @@ fn void bitset_list_add(VirtualBuffer(OSEventBitset)* list, u32* counter, u64 va
void os_windowing_init(OSWindowingInitializationOptions options)
{
#ifdef __linux__
use_x11 = options.should_use_x11;
int platform_hint = options.should_use_x11 ? GLFW_PLATFORM_X11 : GLFW_PLATFORM_WAYLAND;
glfwInitHint(GLFW_PLATFORM, platform_hint);
if (platform_hint == GLFW_PLATFORM_X11)
{
glfwInitHint(GLFW_X11_XCB_VULKAN_SURFACE, GLFW_FALSE);
}
#endif
if (glfwInit() != GLFW_TRUE)
@ -436,29 +429,6 @@ OSCursorPosition os_window_cursor_position_get(OSWindow window)
int window_create_surface(void* instance, OSWindow window, const void* allocator, void** surface)
{
auto* surface_pointer = (VkSurfaceKHR*)surface;
#ifdef __linux__
#define FORCE_XLIB_INITIALIZATION 1
if (use_x11 && FORCE_XLIB_INITIALIZATION)
{
auto* x11_display = glfwGetX11Display();
auto x11_window = glfwGetX11Window(window);
VkXlibSurfaceCreateInfoKHR create_info = {
.sType = VK_STRUCTURE_TYPE_XLIB_SURFACE_CREATE_INFO_KHR,
.pNext = 0,
.flags = 0,
.dpy = x11_display,
.window = x11_window,
};
return vkCreateXlibSurfaceKHR(instance, &create_info, allocator, surface_pointer);
}
else
{
#endif
return glfwCreateWindowSurface(instance, window, allocator, surface_pointer);
#ifdef __linux__
}
#endif
return glfwCreateWindowSurface(instance, window, allocator, (VkSurfaceKHR*)surface);
}

View File

@ -111,6 +111,10 @@ ninja -v
cd $ORIGINAL_DIR/build
ln -s $BUILD_DIR/compile_commands.json . || true
cd $ORIGINAL_DIR
if [[ "$BB_IS_CI" == "OFF" ]]; then
glslangValidator -V bootstrap/std/shaders/rect.vert -o $BUILD_DIR/rect.vert.spv
glslangValidator -V bootstrap/std/shaders/rect.frag -o $BUILD_DIR/rect.frag.spv
fi
if [ "$#" -ne 0 ]; then
$BUILD_DIR/runner $@