Skip to content

Commit 370e97e

Browse files
committed
[Feat|Fix] (Shader): Inject atomicCounterAdd and do not remove binding for compute shader.
1 parent ea6bc67 commit 370e97e

1 file changed

Lines changed: 36 additions & 3 deletions

File tree

src/main/cpp/gl/glsl/glsl_for_es.cpp

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,32 @@ vec2 mg_textureQueryLod(sampler2D tex, vec2 uv) {
570570
glsl.insert(insertPos, "\n" + textureQueryLodImpl + "\n");
571571
}
572572

573+
static void inject_atomicCounterAdd(std::string& glsl) {
574+
if (glsl.find("atomicCounterAdd") == std::string::npos) {
575+
return;
576+
}
577+
578+
const std::regex defRegex(R"(uint\s+mg_atomicCounterAdd\s*\()", std::regex::ECMAScript);
579+
if (std::regex_search(glsl, defRegex)) {
580+
return;
581+
}
582+
583+
const std::string atomicCounterAddImpl = R"(
584+
#define atomicCounterAdd mg_atomicCounterAdd
585+
586+
uint mg_atomicCounterAdd(atomic_uint ac, uint val) {
587+
uint old = atomicCounterIncrement(ac) - 1u;
588+
for (uint i = 1u; i < val; ++i) {
589+
atomicCounterIncrement(ac);
590+
}
591+
return old;
592+
}
593+
)";
594+
595+
size_t insertPos = find_insertion_point(glsl);
596+
glsl.insert(insertPos, "\n" + atomicCounterAddImpl + "\n");
597+
}
598+
573599
static inline void inject_temporal_filter(std::string& glsl) {
574600
const std::regex defRegex(R"(vec4\s+GI_TemporalFilter\s*\()", std::regex::ECMAScript);
575601

@@ -637,7 +663,7 @@ void inject_mg_macro_definition(std::string& glslCode) {
637663
}
638664

639665

640-
std::string preprocess_glsl(const std::string& glsl) {
666+
std::string preprocess_glsl(const std::string& glsl, GLenum shaderType) {
641667
std::string ret = glsl;
642668
// Remove lines beginning with `#line`
643669
ret = replace_line_starting_with(ret, "#line");
@@ -665,6 +691,10 @@ std::string preprocess_glsl(const std::string& glsl) {
665691
// Sampler buffer processing
666692
process_sampler_buffer(ret);
667693
}
694+
695+
if (shaderType == GL_COMPUTE_SHADER) {
696+
inject_atomicCounterAdd(ret);
697+
}
668698

669699
return ret;
670700
}
@@ -783,7 +813,7 @@ std::string spirv_to_essl(std::vector<unsigned int> spirv, uint essl_version, in
783813

784814
static bool glslang_inited = false;
785815
std::string GLSLtoGLSLES_2(const char *glsl_code, GLenum glsl_type, uint essl_version, int& return_code) {
786-
std::string correct_glsl_str = preprocess_glsl(glsl_code);
816+
std::string correct_glsl_str = preprocess_glsl(glsl_code, glsl_type);
787817
LOG_D("Firstly converted GLSL:\n%s", correct_glsl_str.c_str())
788818
int glsl_version = get_or_add_glsl_version(correct_glsl_str);
789819

@@ -806,7 +836,10 @@ std::string GLSLtoGLSLES_2(const char *glsl_code, GLenum glsl_type, uint essl_ve
806836
}
807837

808838
// Post-processing ESSL
809-
essl = removeLayoutBinding(essl);
839+
840+
if (glsl_type != GL_COMPUTE_SHADER) {
841+
essl = removeLayoutBinding(essl);
842+
}
810843
essl = processOutColorLocations(essl);
811844
essl = forceSupporterOutput(essl);
812845

0 commit comments

Comments
 (0)