mirror of
https://github.com/cocos/cocos4.git
synced 2026-12-03 17:42:41 +08:00
@@ -844,104 +844,111 @@ void overwriteShaderSourceBinding(
|
||||
const gfx::ShaderInfo &shaderInfo,
|
||||
ccstd::string &source,
|
||||
boost::container::pmr::memory_resource *scratch) {
|
||||
// find first uniform
|
||||
auto pos = source.find(" uniform ");
|
||||
while (pos != ccstd::string::npos) {
|
||||
// uniform statement is separated by ";{}";
|
||||
auto beg = source.find_last_of(";}", pos);
|
||||
auto end = source.find_first_of(";{", pos);
|
||||
if (beg == ccstd::string::npos) {
|
||||
// if separator is not found, start from 0
|
||||
beg = 0;
|
||||
} else {
|
||||
// bypass last separator
|
||||
beg += 1;
|
||||
}
|
||||
// uniform declaration is found
|
||||
CC_EXPECTS(beg != ccstd::string::npos);
|
||||
CC_EXPECTS(end != ccstd::string::npos);
|
||||
CC_ENSURES(end > beg);
|
||||
|
||||
// find uniform name
|
||||
std::string_view name{};
|
||||
{
|
||||
std::string_view decl(source.c_str() + beg, end - beg);
|
||||
auto nameEnd = decl.size();
|
||||
for (; nameEnd-- > 0;) {
|
||||
if (!std::isspace(decl[nameEnd])) {
|
||||
break;
|
||||
const char* keyWords[] = {
|
||||
" uniform ", " buffer "
|
||||
};
|
||||
|
||||
for (auto &keyword : keyWords) {
|
||||
// find first uniform
|
||||
auto pos = source.find(keyword);
|
||||
while (pos != ccstd::string::npos) {
|
||||
// uniform statement is separated by ";{}";
|
||||
auto beg = source.find_last_of(";}", pos);
|
||||
auto end = source.find_first_of(";{", pos);
|
||||
if (beg == ccstd::string::npos) {
|
||||
// if separator is not found, start from 0
|
||||
beg = 0;
|
||||
} else {
|
||||
// bypass last separator
|
||||
beg += 1;
|
||||
}
|
||||
// uniform declaration is found
|
||||
CC_EXPECTS(beg != ccstd::string::npos);
|
||||
CC_EXPECTS(end != ccstd::string::npos);
|
||||
CC_ENSURES(end > beg);
|
||||
|
||||
// find uniform name
|
||||
std::string_view name{};
|
||||
{
|
||||
std::string_view decl(source.c_str() + beg, end - beg);
|
||||
auto nameEnd = decl.size();
|
||||
for (; nameEnd-- > 0;) {
|
||||
if (!std::isspace(decl[nameEnd])) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
auto nameBeg = decl.find_last_of(' ', nameEnd);
|
||||
nameBeg += 1;
|
||||
nameEnd += 1;
|
||||
// uniform name found
|
||||
name = decl.substr(nameBeg, nameEnd - nameBeg);
|
||||
}
|
||||
CC_ENSURES(!name.empty());
|
||||
|
||||
// layout
|
||||
std::ptrdiff_t offset = 0;
|
||||
{
|
||||
std::string_view prevLayout;
|
||||
// find layout expression
|
||||
auto layoutBeg = source.rfind("layout", pos);
|
||||
auto layoutEnd = ccstd::string::npos;
|
||||
if (layoutBeg == ccstd::string::npos || layoutBeg < beg) {
|
||||
// layout not found
|
||||
layoutBeg = ccstd::string::npos;
|
||||
CC_ENSURES(layoutBeg == ccstd::string::npos);
|
||||
CC_ENSURES(layoutEnd == ccstd::string::npos);
|
||||
} else {
|
||||
CC_EXPECTS(layoutBeg >= beg && layoutBeg <= end);
|
||||
layoutEnd = source.find(')', layoutBeg) + 1;
|
||||
CC_EXPECTS(layoutEnd != ccstd::string::npos);
|
||||
CC_ENSURES(layoutBeg < layoutEnd && layoutEnd <= end);
|
||||
prevLayout = std::string_view(source.c_str() + layoutBeg, layoutEnd - layoutBeg);
|
||||
|
||||
// check layout expression is within uniform declaration
|
||||
// prev layout expression is from layoutBeg to layoutEnd
|
||||
CC_ENSURES(layoutBeg >= beg && layoutBeg <= end);
|
||||
CC_ENSURES(layoutEnd >= layoutBeg && layoutEnd <= end);
|
||||
}
|
||||
|
||||
// find uniform set and binding
|
||||
auto [set, binding] = findBinding(shaderInfo, name);
|
||||
|
||||
// compose new layout expression
|
||||
ccstd::pmr::string newLayout(scratch);
|
||||
newLayout.reserve(32);
|
||||
newLayout.append("layout(set = ");
|
||||
newLayout.append(std::to_string(set));
|
||||
newLayout.append(", binding = ");
|
||||
newLayout.append(std::to_string(binding));
|
||||
|
||||
auto inputIndex = prevLayout.find("input_attachment_index");
|
||||
if (inputIndex != ccstd::string::npos) {
|
||||
newLayout.append(", ");
|
||||
auto endIndex = prevLayout.find_first_of(",)", inputIndex + 1);
|
||||
newLayout.append(prevLayout.data(), inputIndex, endIndex - inputIndex);
|
||||
}
|
||||
|
||||
newLayout.append(")");
|
||||
|
||||
// replace layout expression
|
||||
if (layoutBeg == ccstd::string::npos) { // layout not found
|
||||
source.insert(pos, newLayout);
|
||||
offset = static_cast<std::ptrdiff_t>(newLayout.size());
|
||||
} else {
|
||||
// layout is found
|
||||
source.erase(layoutBeg, prevLayout.size());
|
||||
source.insert(layoutBeg, newLayout);
|
||||
// calculate string difference
|
||||
offset = static_cast<std::ptrdiff_t>(newLayout.size()) -
|
||||
static_cast<std::ptrdiff_t>(prevLayout.size());
|
||||
}
|
||||
}
|
||||
auto nameBeg = decl.find_last_of(' ', nameEnd);
|
||||
nameBeg += 1;
|
||||
nameEnd += 1;
|
||||
// uniform name found
|
||||
name = decl.substr(nameBeg, nameEnd - nameBeg);
|
||||
// offset end of uniform declaration, offset is caused by modification of layout
|
||||
end += offset;
|
||||
// find next uniform
|
||||
pos = source.find(keyword, end);
|
||||
}
|
||||
CC_ENSURES(!name.empty());
|
||||
|
||||
// layout
|
||||
std::ptrdiff_t offset = 0;
|
||||
{
|
||||
std::string_view prevLayout;
|
||||
// find layout expression
|
||||
auto layoutBeg = source.rfind("layout", pos);
|
||||
auto layoutEnd = ccstd::string::npos;
|
||||
if (layoutBeg == ccstd::string::npos || layoutBeg < beg) {
|
||||
// layout not found
|
||||
layoutBeg = ccstd::string::npos;
|
||||
CC_ENSURES(layoutBeg == ccstd::string::npos);
|
||||
CC_ENSURES(layoutEnd == ccstd::string::npos);
|
||||
} else {
|
||||
CC_EXPECTS(layoutBeg >= beg && layoutBeg <= end);
|
||||
layoutEnd = source.find(')', layoutBeg) + 1;
|
||||
CC_EXPECTS(layoutEnd != ccstd::string::npos);
|
||||
CC_ENSURES(layoutBeg < layoutEnd && layoutEnd <= end);
|
||||
prevLayout = std::string_view(source.c_str() + layoutBeg, layoutEnd - layoutBeg);
|
||||
|
||||
// check layout expression is within uniform declaration
|
||||
// prev layout expression is from layoutBeg to layoutEnd
|
||||
CC_ENSURES(layoutBeg >= beg && layoutBeg <= end);
|
||||
CC_ENSURES(layoutEnd >= layoutBeg && layoutEnd <= end);
|
||||
}
|
||||
|
||||
// find uniform set and binding
|
||||
auto [set, binding] = findBinding(shaderInfo, name);
|
||||
|
||||
// compose new layout expression
|
||||
ccstd::pmr::string newLayout(scratch);
|
||||
newLayout.reserve(32);
|
||||
newLayout.append("layout(set = ");
|
||||
newLayout.append(std::to_string(set));
|
||||
newLayout.append(", binding = ");
|
||||
newLayout.append(std::to_string(binding));
|
||||
|
||||
auto inputIndex = prevLayout.find("input_attachment_index");
|
||||
if (inputIndex != ccstd::string::npos) {
|
||||
newLayout.append(", ");
|
||||
auto endIndex = prevLayout.find_first_of(",)", inputIndex + 1);
|
||||
newLayout.append(prevLayout.data(), inputIndex, endIndex - inputIndex);
|
||||
}
|
||||
|
||||
newLayout.append(")");
|
||||
|
||||
// replace layout expression
|
||||
if (layoutBeg == ccstd::string::npos) { // layout not found
|
||||
source.insert(pos, newLayout);
|
||||
offset = static_cast<std::ptrdiff_t>(newLayout.size());
|
||||
} else {
|
||||
// layout is found
|
||||
source.erase(layoutBeg, prevLayout.size());
|
||||
source.insert(layoutBeg, newLayout);
|
||||
// calculate string difference
|
||||
offset = static_cast<std::ptrdiff_t>(newLayout.size()) -
|
||||
static_cast<std::ptrdiff_t>(prevLayout.size());
|
||||
}
|
||||
}
|
||||
// offset end of uniform declaration, offset is caused by modification of layout
|
||||
end += offset;
|
||||
// find next uniform
|
||||
pos = source.find(" uniform ", end);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user