mirror of
https://github.com/MobileGL-Dev/MobileGL
synced 2026-09-12 06:08:30 +09:00
[Fix] (Diligent/Shader): Rename glsl builtins for Vulkan.
This commit is contained in:
@@ -371,6 +371,7 @@ namespace MG_Diligent {
|
|||||||
|
|
||||||
shaderSource = MG_Util::Program::BindInputLayoutLocationsForGLSL(spirv,
|
shaderSource = MG_Util::Program::BindInputLayoutLocationsForGLSL(spirv,
|
||||||
programObj.attribLocations);
|
programObj.attribLocations);
|
||||||
|
MG_Util::Program::RenameGLSLBuiltinsForVulkan(shaderSource);
|
||||||
} else {
|
} else {
|
||||||
shaderSource = it->second.source;
|
shaderSource = it->second.source;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -277,6 +277,7 @@ namespace MG_GL::GL {
|
|||||||
} else {
|
} else {
|
||||||
shaderSource = it->second.source;
|
shaderSource = it->second.source;
|
||||||
}
|
}
|
||||||
|
MG_Util::Program::RenameGLSLBuiltinsForVulkan(shaderSource);
|
||||||
shaderSources[it->first] = shaderSource;
|
shaderSources[it->first] = shaderSource;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -176,9 +176,25 @@ GLint ProgramState::QueryProgramAttributeLocation(GLuint program, const GLchar*
|
|||||||
GLint loc = it->second;
|
GLint loc = it->second;
|
||||||
MG_Util::Debug::LogD("MG_State: Program: QueryProgramAttributeLocation success: %s -> %d", name, loc);
|
MG_Util::Debug::LogD("MG_State: Program: QueryProgramAttributeLocation success: %s -> %d", name, loc);
|
||||||
return loc;
|
return loc;
|
||||||
|
} else {
|
||||||
|
MG_Util::Debug::LogW("MG_State: Program: QueryProgramAttributeLocation warning: name '%s' not found, generating new one...", name);
|
||||||
|
// Find the next available location
|
||||||
|
GLint loc = (GLint)prog.attribLocations.size();
|
||||||
|
bool locInUse;
|
||||||
|
do {
|
||||||
|
locInUse = false;
|
||||||
|
for (const auto& pair : prog.attribLocations) {
|
||||||
|
if (pair.second == loc) {
|
||||||
|
locInUse = true;
|
||||||
|
loc++;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} while (locInUse);
|
||||||
|
prog.attribLocations[name] = loc;
|
||||||
|
MG_Util::Debug::LogD("MG_State: Program: QueryProgramAttributeLocation success: %s -> %d", name, loc);
|
||||||
|
return prog.attribLocations[name];
|
||||||
}
|
}
|
||||||
MG_Util::Debug::LogW("MG_State: Program: QueryProgramAttributeLocation warning: name '%s' not found", name);
|
|
||||||
return -1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
GLenum ProgramState::QueryProgramStateIntVector(GLuint program, GLenum pname, GLint* params) {
|
GLenum ProgramState::QueryProgramStateIntVector(GLuint program, GLenum pname, GLint* params) {
|
||||||
|
|||||||
@@ -5,8 +5,19 @@
|
|||||||
#include "GLSLTool.h"
|
#include "GLSLTool.h"
|
||||||
|
|
||||||
namespace MG_Util::Program {
|
namespace MG_Util::Program {
|
||||||
std::string BindInputLayoutLocationsForGLSL(const std::vector<uint32_t>& spirv,
|
void RenameGLSLBuiltinsForVulkan(std::string &src) {
|
||||||
const MG_Global::unordered_map<std::string, GLint>&
|
static const std::vector<std::pair<std::regex, std::string>> rules = {
|
||||||
|
{ std::regex(R"(\bgl_VertexID\b)"), "gl_VertexIndex" },
|
||||||
|
{ std::regex(R"(\bgl_InstanceID\b)"), "gl_InstanceIndex" }
|
||||||
|
};
|
||||||
|
|
||||||
|
for (auto &rule : rules) {
|
||||||
|
src = std::regex_replace(src, rule.first, rule.second);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string BindInputLayoutLocationsForGLSL(std::vector<uint32_t>& spirv,
|
||||||
|
MG_Global::unordered_map<std::string, GLint>&
|
||||||
name_location_map) {
|
name_location_map) {
|
||||||
spvc_context context = nullptr;
|
spvc_context context = nullptr;
|
||||||
spvc_parsed_ir ir = nullptr;
|
spvc_parsed_ir ir = nullptr;
|
||||||
@@ -17,11 +28,13 @@ namespace MG_Util::Program {
|
|||||||
std::string output_glsl;
|
std::string output_glsl;
|
||||||
|
|
||||||
if ((result = spvc_context_create(&context)) != SPVC_SUCCESS) {
|
if ((result = spvc_context_create(&context)) != SPVC_SUCCESS) {
|
||||||
|
MG_Util::Debug::LogE("[SPIRV-Cross] Failed to create context.");
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((result = spvc_context_parse_spirv(context, spirv.data(), spirv.size(), &ir)) != SPVC_SUCCESS) {
|
if ((result = spvc_context_parse_spirv(context, spirv.data(), spirv.size(), &ir)) != SPVC_SUCCESS) {
|
||||||
spvc_context_destroy(context);
|
spvc_context_destroy(context);
|
||||||
|
MG_Util::Debug::LogE("[SPIRV-Cross] Failed to parse SPIR-V.");
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -33,11 +46,13 @@ namespace MG_Util::Program {
|
|||||||
&compiler
|
&compiler
|
||||||
)) != SPVC_SUCCESS) {
|
)) != SPVC_SUCCESS) {
|
||||||
spvc_context_destroy(context);
|
spvc_context_destroy(context);
|
||||||
|
MG_Util::Debug::LogE("[SPIRV-Cross] Failed to create compiler.");
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((result = spvc_compiler_create_shader_resources(compiler, &resources)) != SPVC_SUCCESS) {
|
if ((result = spvc_compiler_create_shader_resources(compiler, &resources)) != SPVC_SUCCESS) {
|
||||||
spvc_context_destroy(context);
|
spvc_context_destroy(context);
|
||||||
|
MG_Util::Debug::LogE("[SPIRV-Cross] Failed to create shader resources.");
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -50,6 +65,7 @@ namespace MG_Util::Program {
|
|||||||
&num_inputs
|
&num_inputs
|
||||||
)) != SPVC_SUCCESS) {
|
)) != SPVC_SUCCESS) {
|
||||||
spvc_context_destroy(context);
|
spvc_context_destroy(context);
|
||||||
|
MG_Util::Debug::LogE("[SPIRV-Cross] Failed to get stage inputs.");
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -61,8 +77,8 @@ namespace MG_Util::Program {
|
|||||||
for (const auto& [name, location] : name_location_map) {
|
for (const auto& [name, location] : name_location_map) {
|
||||||
auto it = name_to_id.find(name);
|
auto it = name_to_id.find(name);
|
||||||
if (it == name_to_id.end()) {
|
if (it == name_to_id.end()) {
|
||||||
spvc_context_destroy(context);
|
MG_Util::Debug::LogW("[SPIRV-Cross] Input name not found in shader: %s", name.c_str());
|
||||||
return {};
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
spvc_compiler_set_decoration(
|
spvc_compiler_set_decoration(
|
||||||
@@ -81,6 +97,8 @@ namespace MG_Util::Program {
|
|||||||
|
|
||||||
if ((result = spvc_compiler_compile(compiler, &glsl_source)) != SPVC_SUCCESS) {
|
if ((result = spvc_compiler_compile(compiler, &glsl_source)) != SPVC_SUCCESS) {
|
||||||
spvc_context_destroy(context);
|
spvc_context_destroy(context);
|
||||||
|
MG_Util::Debug::LogE("[SPIRV-Cross] Failed to compile to GLSL: %s",
|
||||||
|
spvc_context_get_last_error_string(context));
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -182,9 +182,10 @@ namespace MG_Util::Program {
|
|||||||
std::string>& glslSources);
|
std::string>& glslSources);
|
||||||
void GenerateDefaultUBOForGLSL_Multi(
|
void GenerateDefaultUBOForGLSL_Multi(
|
||||||
MG_Global::unordered_map<GLuint, std::string> &shaderSources, std::vector<std::string>& outUniformBufferNames);
|
MG_Global::unordered_map<GLuint, std::string> &shaderSources, std::vector<std::string>& outUniformBufferNames);
|
||||||
std::string BindInputLayoutLocationsForGLSL(const std::vector<uint32_t>& spirv,
|
std::string BindInputLayoutLocationsForGLSL(std::vector<uint32_t>& spirv,
|
||||||
const MG_Global::unordered_map<std::string, GLint>&
|
MG_Global::unordered_map<std::string, GLint>&
|
||||||
name_location_map);
|
name_location_map);
|
||||||
|
void RenameGLSLBuiltinsForVulkan(std::string &src);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif //MOBILEGL_GLSLTOOL_H
|
#endif //MOBILEGL_GLSLTOOL_H
|
||||||
|
|||||||
Reference in New Issue
Block a user