Skip to content

Commit 0ce72c3

Browse files
committed
feat: spirv can now optionally include path to original glsl file as well as debug information
1 parent 606307c commit 0ce72c3

2 files changed

Lines changed: 28 additions & 8 deletions

File tree

include/misc/glsl_to_spirv.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
#include "misc/callbacks.h"
3333
#include "misc/debug.h"
3434
#include "misc/types.h"
35+
#include <string>
36+
#include <optional>
3537
#include <map>
3638
#include <memory>
3739
#include <sstream>
@@ -145,6 +147,9 @@ namespace Anvil
145147
/** Destructor. Releases all created Vulkan objects, as well as the SPIR-V blob data. */
146148
~GLSLShaderToSPIRVGenerator();
147149

150+
void set_glsl_file_path(const std::optional<std::string> &in_glsl_file_path) { m_glsl_file_path = in_glsl_file_path; }
151+
void set_with_debug_info(bool in_with_debug_info) { m_with_debug_info = in_with_debug_info; }
152+
148153
/** Adds a "#define [definition_name] [value]" line after the first newline found in the
149154
* source code.
150155
*
@@ -366,7 +371,7 @@ namespace Anvil
366371
bool bake_glsl_source_code() const;
367372

368373
#ifdef ANVIL_LINK_WITH_GLSLANG
369-
bool bake_spirv_blob_by_calling_glslang(const char* in_body) const;
374+
bool bake_spirv_blob_by_calling_glslang(const char* in_body, const std::optional<std::string> &glslFilePath = {}, bool withDebugInfo = false) const;
370375
EShLanguage get_glslang_shader_stage () const;
371376
#else
372377
bool bake_spirv_blob_by_spawning_glslang_process(const std::string& in_glsl_filename_with_path,
@@ -383,6 +388,8 @@ namespace Anvil
383388
#endif
384389

385390
std::string m_data;
391+
std::optional<std::string> m_glsl_file_path;
392+
bool m_with_debug_info;
386393
Mode m_mode;
387394

388395
mutable std::string m_glsl_source_code;

src/misc/glsl_to_spirv.cpp

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,9 @@ Anvil::GLSLShaderToSPIRVGenerator::GLSLShaderToSPIRVGenerator(const Anvil::BaseD
258258
m_glsl_source_code_dirty(true),
259259
m_mode (in_mode),
260260
m_shader_stage (in_shader_stage),
261-
m_spirv_version (in_spirv_version)
261+
m_spirv_version (in_spirv_version),
262+
m_glsl_file_path{},
263+
m_with_debug_info{false}
262264
{
263265
#ifdef ANVIL_LINK_WITH_GLSLANG
264266
{
@@ -612,7 +614,7 @@ bool Anvil::GLSLShaderToSPIRVGenerator::bake_spirv_blob() const
612614
if (m_spirv_blob.size() == 0)
613615
{
614616
/* Need to bake a brand new SPIR-V blob */
615-
result = bake_spirv_blob_by_calling_glslang(m_glsl_source_code.c_str() );
617+
result = bake_spirv_blob_by_calling_glslang(m_glsl_source_code.c_str(), m_glsl_file_path, m_with_debug_info );
616618
}
617619
}
618620

@@ -639,7 +641,7 @@ bool Anvil::GLSLShaderToSPIRVGenerator::bake_spirv_blob() const
639641
*
640642
* @return true if successful, false otherwise.
641643
**/
642-
bool Anvil::GLSLShaderToSPIRVGenerator::bake_spirv_blob_by_calling_glslang(const char* in_body) const
644+
bool Anvil::GLSLShaderToSPIRVGenerator::bake_spirv_blob_by_calling_glslang(const char* in_body, const std::optional<std::string> &glslFilePath, bool withDebugInfo) const
643645
{
644646
const EShLanguage glslang_shader_stage = get_glslang_shader_stage();
645647
glslang::TIntermediate* intermediate_ptr = nullptr;
@@ -669,8 +671,14 @@ bool Anvil::GLSLShaderToSPIRVGenerator::bake_spirv_blob() const
669671
glslang::EShTargetLanguageVersion spirv_version;
670672

671673
/* Try to compile the shader */
672-
new_shader_ptr->setStrings(&in_body,
673-
1);
674+
const char *path = nullptr;
675+
int numNames = 0;
676+
if(glslFilePath) {
677+
path = glslFilePath->c_str();
678+
numNames = 1;
679+
}
680+
int length = strlen(in_body);
681+
new_shader_ptr->setStringsWithLengthsAndNames(&in_body, &length, &path, numNames);
674682

675683
switch (m_spirv_version)
676684
{
@@ -723,7 +731,7 @@ bool Anvil::GLSLShaderToSPIRVGenerator::bake_spirv_blob() const
723731
result = new_shader_ptr->parse(m_limits_ptr->get_resource_ptr(),
724732
110, /* defaultVersion */
725733
false, /* forwardCompatible */
726-
(EShMessages) (EShMsgDefault | EShMsgSpvRules | EShMsgVulkanRules) );
734+
(EShMessages) (EShMsgDefault | EShMsgSpvRules | EShMsgVulkanRules | EShMsgDebugInfo) );
727735

728736
m_debug_info_log = new_shader_ptr->getInfoDebugLog();
729737
m_shader_info_log = new_shader_ptr->getInfoLog();
@@ -765,8 +773,13 @@ bool Anvil::GLSLShaderToSPIRVGenerator::bake_spirv_blob() const
765773
goto end;
766774
}
767775

776+
glslang::SpvOptions options {};
777+
if(withDebugInfo) {
778+
options.generateDebugInfo = true;
779+
options.stripDebugInfo = false;
780+
}
768781
glslang::GlslangToSpv(*intermediate_ptr,
769-
spirv_blob);
782+
spirv_blob, &options);
770783

771784
if (spirv_blob.size() == 0)
772785
{

0 commit comments

Comments
 (0)