From e52813f93ba1cd31eb4e9a890dca3f2659183cac Mon Sep 17 00:00:00 2001 From: Jonathan Hoffstadt Date: Wed, 5 Jun 2024 22:54:36 -0500 Subject: [PATCH] WIP2 --- extensions/pl_ref_renderer_ext.c | 44 +++++++++++++++++++++++++++++--- scripts/gen_build.py | 12 +++++---- shaders/glsl/primitive.frag | 4 +-- 3 files changed, 50 insertions(+), 10 deletions(-) diff --git a/extensions/pl_ref_renderer_ext.c b/extensions/pl_ref_renderer_ext.c index 8f6ca0e5..94246056 100644 --- a/extensions/pl_ref_renderer_ext.c +++ b/extensions/pl_ref_renderer_ext.c @@ -41,6 +41,8 @@ Index of this file: #include "pl_draw_ext.h" #include "pl_ui_ext.h" +#include "shaderc/shaderc.h" + #define PL_MAX_VIEWS_PER_SCENE 4 #define PL_MAX_LIGHTS 1000 @@ -454,6 +456,29 @@ static plBufferHandle pl__refr_create_local_buffer (const plBufferDescr // [SECTION] implementation //----------------------------------------------------------------------------- +shaderc_include_result* +pl_shaderc_include_resolve_fn(void* user_data, const char* requested_source, int type, const char* requesting_source, size_t include_depth) +{ + shaderc_include_result* result = PL_ALLOC(sizeof(shaderc_include_result)); + uint32_t uVertexShaderSize = 0; + gptFile->read(requested_source, &uVertexShaderSize, NULL, "rb"); + uint8_t* puVertexShaderCode = PL_ALLOC((size_t)uVertexShaderSize); + gptFile->read(requested_source, &uVertexShaderSize, puVertexShaderCode, "rb"); + + result->source_name = requested_source; + result->source_name_length = strlen(requested_source); + result->content = (const char*)puVertexShaderCode; + result->content_length = (size_t)uVertexShaderSize; + result->user_data = user_data; + return result; + +} + +void pl_shaderc_include_result_release_fn(void* user_data, shaderc_include_result* include_result) +{ + +} + static void pl_refr_initialize(plWindow* ptWindow) { @@ -666,7 +691,12 @@ pl_refr_initialize(plWindow* ptWindow) const char* pcShader = "../shaders/metal/primitive.metal"; #else const char* pcVertexShader = "primitive.vert.spv"; - const char* pcPixelShader = "primitive.frag.spv"; + // const char* pcPixelShader = "primitive.frag.spv"; + const char* pcPixelShader = "../shaders/glsl/primitive.frag"; + + shaderc_compiler_t tBlah = shaderc_compiler_initialize(); + shaderc_compile_options_t tOptions = shaderc_compile_options_initialize(); + shaderc_compile_options_set_include_callbacks(tOptions, pl_shaderc_include_resolve_fn, pl_shaderc_include_result_release_fn, NULL); gptFile->read(pcVertexShader, &uVertexShaderSize, NULL, "rb"); gptFile->read(pcPixelShader, &uPixelShaderSize, NULL, "rb"); @@ -676,13 +706,21 @@ pl_refr_initialize(plWindow* ptWindow) gptFile->read(pcPixelShader, &uPixelShaderSize, puPixelShaderCode, "rb"); pl_sb_push(gptData->sbptShaderBytecodeCache, puVertexShaderCode); pl_sb_push(gptData->sbptShaderBytecodeCache, puPixelShaderCode); + + shaderc_compilation_result_t tresult = shaderc_compile_into_spv(tBlah, (const char*)puPixelShaderCode, (size_t)uPixelShaderSize, shaderc_glsl_fragment_shader, "primitive.frag", "main", tOptions); + size_t uNumErrors = shaderc_result_get_num_errors(tresult); + if(uNumErrors) + { + printf("%s\n", shaderc_result_get_error_message(tresult)); + } #endif plShaderDescription tOpaqueShaderDescription = { .szVertexShaderByteCodeSize = uVertexShaderSize, - .szPixelShaderByteCodeSize = uPixelShaderSize, + .szPixelShaderByteCodeSize = shaderc_result_get_length(tresult), .puVertexShaderByteCode = puVertexShaderCode, - .puPixelShaderByteCode = puPixelShaderCode, + .puPixelShaderByteCode = (uint8_t*)shaderc_result_get_bytes(tresult), + .tGraphicsState = { .ulDepthWriteEnabled = 1, .ulDepthMode = PL_COMPARE_MODE_LESS, diff --git a/scripts/gen_build.py b/scripts/gen_build.py index 7f88d03a..0de97580 100644 --- a/scripts/gen_build.py +++ b/scripts/gen_build.py @@ -11,8 +11,6 @@ def add_plugin_to_vulkan_app(name, reloadable, binary_name = None, directory = "../extensions/", *kwargs): - pl.push_profile(pl.Profile.VULKAN) - pl.push_definitions("PL_VULKAN_BACKEND") with pl.target(name, pl.TargetType.DYNAMIC_LIBRARY, reloadable): if binary_name is None: pl.push_output_binary(name) @@ -35,8 +33,6 @@ def add_plugin_to_vulkan_app(name, reloadable, binary_name = None, directory = " pass pl.pop_output_binary() pl.pop_source_files() - pl.pop_profile() - pl.pop_definitions() def add_plugin_to_metal_app(name, reloadable, objc = False, binary_name = None, directory = "../extensions/"): @@ -118,7 +114,6 @@ def add_plugin_to_metal_app(name, reloadable, objc = False, binary_name = None, add_plugin_to_vulkan_app("pl_draw_ext", True) add_plugin_to_vulkan_app("pl_debug_ext", False) add_plugin_to_vulkan_app("pl_image_ext", False) - add_plugin_to_vulkan_app("pl_graphics_vulkan", False, "pl_graphics_ext") add_plugin_to_vulkan_app("pl_stats_ext", False) add_plugin_to_vulkan_app("pl_job_ext", False) add_plugin_to_vulkan_app("pl_model_loader_ext", False) @@ -128,6 +123,12 @@ def add_plugin_to_metal_app(name, reloadable, objc = False, binary_name = None, add_plugin_to_vulkan_app("pl_ref_renderer_ext", True) add_plugin_to_vulkan_app("pl_ui_ext", True, None) + pl.push_profile(pl.Profile.VULKAN) + pl.push_definitions("PL_VULKAN_BACKEND") + add_plugin_to_vulkan_app("pl_graphics_vulkan", False, "pl_graphics_ext") + pl.pop_profile() + pl.pop_definitions() + add_plugin_to_metal_app("pl_draw_ext", True) add_plugin_to_metal_app("pl_debug_ext", False) add_plugin_to_metal_app("pl_image_ext", False) @@ -218,6 +219,7 @@ def add_plugin_to_metal_app(name, reloadable, objc = False, binary_name = None, with pl.platform(pl.PlatformType.WIN32): with pl.compiler("msvc", pl.CompilerType.MSVC): pl.add_definition("PL_VULKAN_BACKEND") + pl.add_link_library("shaderc_combined.lib") with pl.platform(pl.PlatformType.LINUX): with pl.compiler("gcc", pl.CompilerType.GCC): pl.add_definition("PL_VULKAN_BACKEND") diff --git a/shaders/glsl/primitive.frag b/shaders/glsl/primitive.frag index 531e9799..33e81ca0 100644 --- a/shaders/glsl/primitive.frag +++ b/shaders/glsl/primitive.frag @@ -1,8 +1,8 @@ #version 450 #extension GL_ARB_separate_shader_objects : enable -#include "defines.glsl" -#include "material.glsl" +#include "../shaders/glsl/defines.glsl" +#include "../shaders/glsl/material.glsl" //----------------------------------------------------------------------------- // [SECTION] specialication constants