Skip to content

Commit 42ff6c7

Browse files
[hgi] On shader compiling errors, output ALL source with line numbers.
The normal hgi behaviour is to log just the reported errors. To make these errors faster to deal with, the shader compiling logs the full source, annotated with the same line numbers as the driver error messages. This output will only be seen if there is a shader compiling error. hgi GL, Vk, and Metal have been patched to do the same thing. Fixes #4048
1 parent 4ed6128 commit 42ff6c7

3 files changed

Lines changed: 53 additions & 4 deletions

File tree

pxr/imaging/hgiGL/shaderFunction.cpp

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
#include "pxr/imaging/hgiGL/shaderFunction.h"
1313
#include "pxr/imaging/hgiGL/shaderGenerator.h"
1414

15+
#include <string>
16+
#include <sstream>
17+
1518
PXR_NAMESPACE_OPEN_SCOPE
1619

1720
HgiGLShaderFunction::HgiGLShaderFunction(
@@ -44,9 +47,26 @@ HgiGLShaderFunction::HgiGLShaderFunction(
4447
if (status != GL_TRUE) {
4548
int logSize = 0;
4649
glGetShaderiv(_shaderId, GL_INFO_LOG_LENGTH, &logSize);
47-
_errors.resize(logSize+1);
48-
glGetShaderInfoLog(_shaderId, logSize, NULL, &_errors[0]);
50+
51+
std::string errors;
52+
errors.resize(logSize+1);
53+
glGetShaderInfoLog(_shaderId, logSize, NULL, &errors[0]);
4954
glDeleteShader(_shaderId);
55+
56+
_errors = "#### Source\n";
57+
{
58+
std::stringstream stream(shaderCode);
59+
size_t indexLine = 1;
60+
61+
std::string line;
62+
while(std::getline(stream, line)) {
63+
_errors += std::to_string(indexLine) + ":" + line + "\n";
64+
indexLine += 1;
65+
}
66+
}
67+
_errors += "#### Errors\n";
68+
_errors += errors;
69+
5070
_shaderId = 0;
5171
}
5272

pxr/imaging/hgiMetal/shaderFunction.mm

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
#include "pxr/base/tf/diagnostic.h"
1515

1616
#include <unordered_map>
17+
#include <string>
18+
#include <sstream>
1719

1820
PXR_NAMESPACE_OPEN_SCOPE
1921

@@ -78,7 +80,19 @@
7880
_shaderId = [library newFunctionWithName:entryPoint];
7981
if (!_shaderId) {
8082
NSString *err = [error localizedDescription];
81-
_errors = [err UTF8String];
83+
_errors = "#### Source\n";
84+
{
85+
std::stringstream stream(shaderCode);
86+
size_t indexLine = 1;
87+
88+
std::string line;
89+
while(std::getline(stream, line)) {
90+
_errors += std::to_string(indexLine) + ":" + line + "\n";
91+
indexLine += 1;
92+
}
93+
}
94+
_errors += "#### Errors\n";
95+
_errors += [err UTF8String];
8296
}
8397
else {
8498
HGIMETAL_DEBUG_LABEL(_shaderId, _descriptor.debugName.c_str());

pxr/imaging/hgiVulkan/shaderCompiler.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
#include <shaderc/shaderc.hpp>
1515

1616
#include <unordered_map>
17+
#include <string>
18+
#include <sstream>
1719

1820
PXR_NAMESPACE_OPEN_SCOPE
1921

@@ -75,7 +77,20 @@ HgiVulkanCompileGLSL(
7577
compiler.CompileGlslToSpv(source, kind, name, options);
7678

7779
if (result.GetCompilationStatus() != shaderc_compilation_status_success) {
78-
*errors = result.GetErrorMessage();
80+
*errors = std::string();
81+
*errors += "#### Source\n";
82+
{
83+
std::stringstream stream(source);
84+
size_t indexLine = 1;
85+
86+
std::string line;
87+
while(std::getline(stream, line)) {
88+
*errors += std::to_string(indexLine) + ":" + line + "\n";
89+
indexLine += 1;
90+
}
91+
}
92+
*errors += "#### Errors\n";
93+
*errors += result.GetErrorMessage();
7994
return false;
8095
}
8196

0 commit comments

Comments
 (0)