Use custom callbacks if they are available in 'i->callbacks'

This commit is contained in:
Alexey Ozeritskiy 2023-04-09 15:06:18 +02:00 committed by kd-11
parent 910542a2cd
commit 6929ac54eb
2 changed files with 37 additions and 52 deletions

View File

@ -80,25 +80,6 @@ typedef struct glslang_program_s {
(CallbackIncluder::callbacks::free_include_result)
*/
class CallbackIncluder : public glslang::TShader::Includer {
public:
/* Wrapper of IncludeResult which stores a glsl_include_result object internally */
class CallbackIncludeResult : public glslang::TShader::Includer::IncludeResult {
public:
CallbackIncludeResult(const std::string& headerName, const char* const headerData, const size_t headerLength,
void* userData, glsl_include_result_t* includeResult)
: glslang::TShader::Includer::IncludeResult(headerName, headerData, headerLength, userData),
includeResult(includeResult)
{
}
virtual ~CallbackIncludeResult() {}
protected:
friend class CallbackIncluder;
glsl_include_result_t* includeResult;
};
public:
CallbackIncluder(glsl_include_callbacks_t _callbacks, void* _context) : callbacks(_callbacks), context(_context) {}
@ -110,9 +91,7 @@ public:
if (this->callbacks.include_system) {
glsl_include_result_t* result =
this->callbacks.include_system(this->context, headerName, includerName, inclusionDepth);
return new CallbackIncludeResult(std::string(headerName), result->header_data, result->header_length,
nullptr, result);
return makeIncludeResult(result);
}
return glslang::TShader::Includer::includeSystem(headerName, includerName, inclusionDepth);
@ -124,9 +103,7 @@ public:
if (this->callbacks.include_local) {
glsl_include_result_t* result =
this->callbacks.include_local(this->context, headerName, includerName, inclusionDepth);
return new CallbackIncludeResult(std::string(headerName), result->header_data, result->header_length,
nullptr, result);
return makeIncludeResult(result);
}
return glslang::TShader::Includer::includeLocal(headerName, includerName, inclusionDepth);
@ -139,22 +116,25 @@ public:
if (result == nullptr)
return;
if (this->callbacks.free_include_result && (result->userData == nullptr)) {
CallbackIncludeResult* innerResult = static_cast<CallbackIncludeResult*>(result);
/* use internal free() function */
this->callbacks.free_include_result(this->context, innerResult->includeResult);
/* ignore internal fields of TShader::Includer::IncludeResult */
delete result;
return;
if (this->callbacks.free_include_result) {
this->callbacks.free_include_result(this->context, static_cast<glsl_include_result_t*>(result->userData));
}
delete[] static_cast<char*>(result->userData);
delete result;
}
private:
CallbackIncluder() {}
IncludeResult* makeIncludeResult(glsl_include_result_t* result) {
if (!result) {
return nullptr;
}
return new glslang::TShader::Includer::IncludeResult(
std::string(result->header_name), result->header_data, result->header_length, result);
}
/* C callback pointers */
glsl_include_callbacks_t callbacks;
/* User-defined context */
@ -394,8 +374,11 @@ GLSLANG_EXPORT const char* glslang_shader_get_preprocessed_code(glslang_shader_t
GLSLANG_EXPORT int glslang_shader_preprocess(glslang_shader_t* shader, const glslang_input_t* input)
{
DirStackFileIncluder Includer;
/* TODO: use custom callbacks if they are available in 'i->callbacks' */
DirStackFileIncluder dirStackFileIncluder;
CallbackIncluder callbackIncluder(input->callbacks, input->callbacks_ctx);
glslang::TShader::Includer& Includer = (input->callbacks.include_local||input->callbacks.include_system)
? static_cast<glslang::TShader::Includer&>(callbackIncluder)
: static_cast<glslang::TShader::Includer&>(dirStackFileIncluder);
return shader->shader->preprocess(
reinterpret_cast<const TBuiltInResource*>(input->resource),
input->default_version,

View File

@ -168,23 +168,6 @@ typedef struct glslang_resource_s {
glslang_limits_t limits;
} glslang_resource_t;
typedef struct glslang_input_s {
glslang_source_t language;
glslang_stage_t stage;
glslang_client_t client;
glslang_target_client_version_t client_version;
glslang_target_language_t target_language;
glslang_target_language_version_t target_language_version;
/** Shader source code */
const char* code;
int default_version;
glslang_profile_t default_profile;
int force_default_version_and_profile;
int forward_compatible;
glslang_messages_t messages;
const glslang_resource_t* resource;
} glslang_input_t;
/* Inclusion result structure allocated by C include_local/include_system callbacks */
typedef struct glsl_include_result_s {
/* Header file name or NULL if inclusion failed */
@ -214,6 +197,25 @@ typedef struct glsl_include_callbacks_s {
glsl_free_include_result_func free_include_result;
} glsl_include_callbacks_t;
typedef struct glslang_input_s {
glslang_source_t language;
glslang_stage_t stage;
glslang_client_t client;
glslang_target_client_version_t client_version;
glslang_target_language_t target_language;
glslang_target_language_version_t target_language_version;
/** Shader source code */
const char* code;
int default_version;
glslang_profile_t default_profile;
int force_default_version_and_profile;
int forward_compatible;
glslang_messages_t messages;
const glslang_resource_t* resource;
glsl_include_callbacks_t callbacks;
void* callbacks_ctx;
} glslang_input_t;
/* SpvOptions counterpart */
typedef struct glslang_spv_options_s {
bool generate_debug_info;