Skip to content

Commit

Permalink
Bump 1.9.3 [release 1.9.3]
Browse files Browse the repository at this point in the history
  • Loading branch information
halx99 committed Aug 23, 2023
1 parent 75f8ea8 commit 77384e9
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 4 deletions.
69 changes: 66 additions & 3 deletions 3rdparty/spirv-cross/spirv_glsl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2295,11 +2295,49 @@ void CompilerGLSL::emit_buffer_block_legacy(const SPIRVariable &var)
auto &block_flags = ir.meta[type.self].decoration.decoration_flags;
bool block_flag = block_flags.get(DecorationBlock);
block_flags.clear(DecorationBlock);
emit_struct(type);
if (!options.emit_expanded_uniforms) {
emit_struct(type);
emit_uniform(var);
statement("");
} else {
emit_buffer_block_expanded(var);
auto pattern = to_name(var.self);
pattern += '.';
expanded_uniform_block_patterns.push_back(std::move(pattern));
}
if (block_flag)
block_flags.set(DecorationBlock);
emit_uniform(var);
statement("");
}

void CompilerGLSL::emit_buffer_block_expanded(const SPIRVariable& var)
{
auto& type = get<SPIRType>(var.basetype);

// Struct types can be stamped out multiple times
// with just different offsets, matrix layouts, etc ...
// Type-punning with these types is legal, which complicates things
// when we are storing struct and array types in an SSBO for example.
// If the type master is packed however, we can no longer assume that the struct declaration will be redundant.
if (type.type_alias != TypeID(0) && !has_extended_decoration(type.type_alias, SPIRVCrossDecorationBufferBlockRepacked))
return;

add_resource_name(type.self);
auto name = type_to_glsl(type);

type.member_name_cache.clear();

uint32_t i = 0;
bool emitted = false;
for (auto& member : type.member_types) {
add_member_name(type, i);
statement_inner("uniform ");
emit_struct_member(type, member, i);
i++;
emitted = true;
}

if (emitted)
statement("");
}

void CompilerGLSL::emit_buffer_reference_block(uint32_t type_id, bool forward_declaration)
Expand Down Expand Up @@ -3701,6 +3739,7 @@ void CompilerGLSL::emit_resources()
}

// Output UBOs and SSBOs
expanded_uniform_block_patterns.clear();
ir.for_each_typed_id<SPIRVariable>([&](uint32_t, SPIRVariable &var) {
auto &type = this->get<SPIRType>(var.basetype);

Expand Down Expand Up @@ -11747,6 +11786,23 @@ CompilerGLSL::TemporaryCopy CompilerGLSL::handle_instruction_precision(const Ins
return {};
}

static int replace_all(std::string& string,
const std::string& replaced_key,
const std::string& replacing_key)
{
if (replaced_key == replacing_key)
return 0;
int count = 0;
std::string::size_type pos = 0;
const size_t predicate = !replaced_key.empty() ? 0 : 1;
while ((pos = string.find(replaced_key, pos)) != std::wstring::npos) {
(void)string.replace(pos, replaced_key.length(), replacing_key);
pos += (replacing_key.length() + predicate);
++count;
}
return count;
}

void CompilerGLSL::emit_instruction(const Instruction &instruction)
{
auto ops = stream(instruction);
Expand Down Expand Up @@ -11836,6 +11892,13 @@ void CompilerGLSL::emit_instruction(const Instruction &instruction)
expr = to_unpacked_expression(ptr);
}

if (options.emit_expanded_uniforms) { // remove uniform block access prefix (xxx.)
const std::string empty_str;
for (auto& pattern : expanded_uniform_block_patterns) {
replace_all(expr, pattern, empty_str);
}
}

auto &type = get<SPIRType>(result_type);
auto &expr_type = expression_type(ptr);

Expand Down
5 changes: 5 additions & 0 deletions 3rdparty/spirv-cross/spirv_glsl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ class CompilerGLSL : public Compiler
// Does not apply to shader storage or push constant blocks.
bool emit_uniform_buffer_as_plain_uniforms = false;

// Whether emit expanded uniforms for legacy GLSL 1.x
bool emit_expanded_uniforms = false;

// Emit OpLine directives if present in the module.
// May not correspond exactly to original source, but should be a good approximation.
bool emit_line_directives = false;
Expand Down Expand Up @@ -655,6 +658,7 @@ class CompilerGLSL : public Compiler
void emit_buffer_block_native(const SPIRVariable &var);
void emit_buffer_reference_block(uint32_t type_id, bool forward_declaration);
void emit_buffer_block_legacy(const SPIRVariable &var);
void emit_buffer_block_expanded(const SPIRVariable& var);
void emit_buffer_block_flattened(const SPIRVariable &type);
void fixup_implicit_builtin_block_names(spv::ExecutionModel model);
void emit_declared_builtin_block(spv::StorageClass storage, spv::ExecutionModel model);
Expand Down Expand Up @@ -1038,6 +1042,7 @@ class CompilerGLSL : public Compiler
void set_composite_constant(ConstantID const_id, TypeID type_id, const SmallVector<ConstantID> &initializers);
TypeID get_composite_member_type(TypeID type_id, uint32_t member_idx);
std::unordered_map<uint32_t, SmallVector<ConstantID>> const_composite_insert_ids;
std::vector<std::string> expanded_uniform_block_patterns; // expanded ub pattern for replace
};
} // namespace SPIRV_CROSS_NAMESPACE

Expand Down
5 changes: 4 additions & 1 deletion src/glslcc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
// Update glslang to: 12.3.1
// Update spirv-corss to: 633dc30 (Aug 17, 2023)
// 1.9.2 Revert `Vertex shader: emit precision qualifiers for essl profile`
// 1.9.3 Expand uniform block members for GLSL/ESSL100
//
#define _ALLOW_KEYWORD_MACROS

Expand Down Expand Up @@ -94,7 +95,7 @@

#define VERSION_MAJOR 1
#define VERSION_MINOR 9
#define VERSION_SUB 2
#define VERSION_SUB 3

static const sx_alloc* g_alloc = sx_alloc_malloc();
static sgs_file* g_sgs = nullptr;
Expand Down Expand Up @@ -1316,6 +1317,7 @@ static int cross_compile(const cmd_args& args, std::vector<uint32_t>& spirv,
}
}

opts.emit_expanded_uniforms = true;
compiler->set_common_options(opts);

std::string code;
Expand Down Expand Up @@ -1811,6 +1813,7 @@ static int compile_files(cmd_args& args, const TBuiltInResource& limits_conf)

glslang::SpvOptions spv_opts;
spv_opts.validate = true;
spv_opts.disableOptimizer = false;
spv::SpvBuildLogger logger;
sx_assert(prog->getIntermediate(files[i].stage));

Expand Down

0 comments on commit 77384e9

Please sign in to comment.