Skip to content

Commit

Permalink
code cleanup (brace style)
Browse files Browse the repository at this point in the history
  • Loading branch information
floooh committed Mar 16, 2024
1 parent 8250b27 commit f6684e6
Show file tree
Hide file tree
Showing 13 changed files with 102 additions and 204 deletions.
15 changes: 5 additions & 10 deletions src/shdc/args.cc
Original file line number Diff line number Diff line change
Expand Up @@ -141,17 +141,15 @@ static void validate(Args& args) {
if (!args.tmpdir.empty()) {
args.tmpdir += "/";
}
}
else {
} else {
if (!pystring::endswith(args.tmpdir, "/")) {
args.tmpdir += "/";
}
}
if (err) {
args.valid = false;
args.exit_code = 10;
}
else {
} else {
args.valid = true;
args.exit_code = 0;
}
Expand All @@ -170,8 +168,7 @@ Args Args::parse(int argc, const char** argv) {
getopt_context_t ctx;
if (getopt_create_context(&ctx, argc, argv, option_list) < 0) {
fmt::print(stderr, "error in getopt_create_context()\n");
}
else {
} else {
int opt = 0;
while ((opt = getopt_next(&ctx)) != -1) {
switch (opt) {
Expand Down Expand Up @@ -232,11 +229,9 @@ Args Args::parse(int argc, const char** argv) {
case OPTION_ERRFMT:
if (0 == strcmp("gcc", ctx.current_opt_arg)) {
args.error_format = ErrMsg::GCC;
}
else if (0 == strcmp("msvc", ctx.current_opt_arg)) {
} else if (0 == strcmp("msvc", ctx.current_opt_arg)) {
args.error_format = ErrMsg::MSVC;
}
else {
} else {
fmt::print(stderr, "sokol-shdc: unknown error format {}, must be 'gcc' or 'msvc'\n", ctx.current_opt_arg);
args.valid = false;
args.exit_code = 10;
Expand Down
39 changes: 13 additions & 26 deletions src/shdc/bytecode.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ static bool write_source(const std::string& source_code, const std::string path)
fwrite(source_code.c_str(), source_code.length(), 1, f);
fclose(f);
return true;
}
else {
} else {
return false;
}
}
Expand All @@ -60,8 +59,7 @@ static bool read_binary(const std::string& path, std::vector<uint8_t>& out_blob)
out_blob = std::move(blob);
}
return true;
}
else {
} else {
out_blob.clear();
return false;
}
Expand Down Expand Up @@ -97,8 +95,7 @@ static void mtl_parse_errors(const std::string& output, const Input& inp, int sn
for (int i = 4; i < (int)tokens.size(); i++) {
if (msg.empty()) {
msg = tokens[i];
}
else {
} else {
msg = fmt::format("{}:{}", msg, tokens[i]);
}
}
Expand All @@ -111,12 +108,10 @@ static void mtl_parse_errors(const std::string& output, const Input& inp, int sn
if (ok) {
if (tokens[3] == " error") {
out_errors.push_back(inp.error(line_index, msg));
}
else {
} else {
out_errors.push_back(inp.warning(line_index, msg));
}
}
else {
} else {
// some error during parsing, output the original line so it isn't lost
out_errors.push_back(inp.error(0, msg));
}
Expand All @@ -129,8 +124,7 @@ static int xcrun(const std::string& cmdline, std::string& output, Slang::Enum sl
std::string cmd = "xcrun ";
if (slang == Slang::METAL_MACOS) {
cmd += "--sdk macosx ";
}
else {
} else {
cmd += "--sdk iphoneos ";
}
cmd += cmdline;
Expand Down Expand Up @@ -158,8 +152,7 @@ static bool mtl_cc(const std::string& src_path, const std::string& out_dia, cons
cmdline += out_air;
if (slang == Slang::METAL_MACOS) {
cmdline += " -mmacosx-version-min=10.11 -std=osx-metal1.1 ";
}
else {
} else {
cmdline += " -miphoneos-version-min=9.0 -std=ios-metal1.1 ";
}
cmdline += src_path;
Expand Down Expand Up @@ -265,8 +258,7 @@ static void d3d_parse_errors(const std::string& output, const Input& inp, int sn
for (int i = 2; i < (int)tokens.size(); i++) {
if (msg.empty()) {
msg = tokens[i];
}
else {
} else {
msg = fmt::format("{}:{}", msg, tokens[i]);
}
}
Expand All @@ -276,12 +268,10 @@ static void d3d_parse_errors(const std::string& output, const Input& inp, int sn
if (ok) {
if (pystring::startswith(tokens[1], " error")) {
out_errors.push_back(ErrMsg::error(inp.base_path, 0, msg));
}
else {
} else {
out_errors.push_back(ErrMsg::warning(inp.base_path, 0, msg));
}
}
else {
} else {
// some error during parsing, output the original line so it isn't lost
out_errors.push_back(ErrMsg::error(inp.base_path, 0, line));
}
Expand All @@ -302,16 +292,13 @@ static Bytecode d3d_compile(const Input& inp, const Spirvcross& spirvcross, Slan
if (slang == Slang::HLSL4) {
if (snippet.type == Snippet::VS) {
compile_target = "vs_4_0";
}
else {
} else {
compile_target = "ps_4_0";
}
}
else {
} else {
if (snippet.type == Snippet::VS) {
compile_target = "vs_5_0";
}
else {
} else {
compile_target = "ps_5_0";
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/shdc/formats/bare.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ static ErrMsg write_stage(const std::string& file_path,
if (blob) {
write_data = blob->data.data();
write_count = blob->data.size();
}
else {
} else {
assert(src);
write_data = src->source_code.data();
write_count = src->source_code.length();
Expand Down
30 changes: 10 additions & 20 deletions src/shdc/formats/sokol.cc
Original file line number Diff line number Diff line change
Expand Up @@ -391,8 +391,7 @@ static void write_shader_sources_and_blobs(const Input& inp,
}
}
L("\n}};\n");
}
else {
} else {
/* if no bytecode exists, write the source code, but also a byte array with a trailing 0 */
std::string c_name = fmt::format("{}{}_source_{}", mod_prefix(inp), snippet.name, Slang::to_str(slang));
const size_t len = src.source_code.length() + 1;
Expand Down Expand Up @@ -422,14 +421,12 @@ static void write_stage(const char* indent,
if (blob) {
L("{}desc.{}.bytecode.ptr = {};\n", indent, stage_name, blob_name);
L("{}desc.{}.bytecode.size = {};\n", indent, stage_name, blob->data.size());
}
else {
} else {
L("{}desc.{}.source = {};\n", indent, stage_name, src_name);
const char* d3d11_tgt = nullptr;
if (slang == Slang::HLSL4) {
d3d11_tgt = (0 == strcmp("vs", stage_name)) ? "vs_4_0" : "ps_4_0";
}
else if (slang == Slang::HLSL5) {
} else if (slang == Slang::HLSL5) {
d3d11_tgt = (0 == strcmp("vs", stage_name)) ? "vs_5_0" : "ps_5_0";
}
if (d3d11_tgt) {
Expand All @@ -447,8 +444,7 @@ static void write_stage(const char* indent,
L("{}desc.{}.uniform_blocks[{}].uniforms[0].name = \"{}\";\n", indent, stage_name, ub_index, ub->struct_name);
L("{}desc.{}.uniform_blocks[{}].uniforms[0].type = {};\n", indent, stage_name, ub_index, uniform_type_to_flattened_sokol_type_str(ub->uniforms[0].type));
L("{}desc.{}.uniform_blocks[{}].uniforms[0].array_count = {};\n", indent, stage_name, ub_index, roundup(ub->size, 16) / 16);
}
else {
} else {
for (int u_index = 0; u_index < (int)ub->uniforms.size(); u_index++) {
const Uniform& u = ub->uniforms[u_index];
L("{}desc.{}.uniform_blocks[{}].uniforms[{}].name = \"{}.{}\";\n", indent, stage_name, ub_index, u_index, ub->inst_name, u.name);
Expand Down Expand Up @@ -498,14 +494,12 @@ static void write_shader_desc_init(const char* indent, const Program& prog, cons
std::string vs_blob_name, fs_blob_name;
if (vs_blob) {
vs_blob_name = fmt::format("{}{}_bytecode_{}", mod_prefix(inp), prog.vs_name, Slang::to_str(slang));
}
else {
} else {
vs_src_name = fmt::format("{}{}_source_{}", mod_prefix(inp), prog.vs_name, Slang::to_str(slang));
}
if (fs_blob) {
fs_blob_name = fmt::format("{}{}_bytecode_{}", mod_prefix(inp), prog.fs_name, Slang::to_str(slang));
}
else {
} else {
fs_src_name = fmt::format("{}{}_source_{}", mod_prefix(inp), prog.fs_name, Slang::to_str(slang));
}

Expand All @@ -515,8 +509,7 @@ static void write_shader_desc_init(const char* indent, const Program& prog, cons
if (attr.slot >= 0) {
if (Slang::is_glsl(slang)) {
L("{}desc.attrs[{}].name = \"{}\";\n", indent, attr_index, attr.name);
}
else if (Slang::is_hlsl(slang)) {
} else if (Slang::is_hlsl(slang)) {
L("{}desc.attrs[{}].sem_name = \"{}\";\n", indent, attr_index, attr.sem_name);
L("{}desc.attrs[{}].sem_index = {};\n", indent, attr_index, attr.sem_index);
}
Expand All @@ -530,8 +523,7 @@ static void write_shader_desc_init(const char* indent, const Program& prog, cons
static std::string func_prefix(const Args& args) {
if (args.output_format != Format::SOKOL_IMPL) {
return std::string("static inline ");
}
else {
} else {
return std::string();
}
}
Expand Down Expand Up @@ -814,8 +806,7 @@ ErrMsg gen(const Args& args, const Input& inp, const std::array<Spirvcross,Slang
guard_written = true;
if (args.output_format == Format::SOKOL_DECL) {
L("#if !defined(SOKOL_SHDC_DECL)\n");
}
else if (args.output_format == Format::SOKOL_IMPL) {
} else if (args.output_format == Format::SOKOL_IMPL) {
L("#if defined(SOKOL_SHDC_IMPL)\n");
}
}
Expand Down Expand Up @@ -854,8 +845,7 @@ ErrMsg gen(const Args& args, const Input& inp, const std::array<Spirvcross,Slang
if (guard_written) {
if (args.output_format == Format::SOKOL_DECL) {
L("#endif /* SOKOL_SHDC_DECL */\n");
}
else if (args.output_format == Format::SOKOL_IMPL) {
} else if (args.output_format == Format::SOKOL_IMPL) {
L("#endif /* SOKOL_SHDC_IMPL */\n");
}
}
Expand Down
30 changes: 10 additions & 20 deletions src/shdc/formats/sokolnim.cc
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,7 @@ static void write_uniform_blocks(const Input& inp, const Spirvcross& spirvcross,
case Uniform::MAT4: L(" {}*{}: array[16, float32]\n", uniform.name, align); break;
default: L(" INVALID_UNIFORM_TYPE\n"); break;
}
}
else {
} else {
switch (uniform.type) {
case Uniform::FLOAT4: L(" {}*{}: array[{}, array[4, float32]]\n", uniform.name, align, uniform.array_count); break;
case Uniform::INT4: L(" {}*{}: array[{}, array[4, int32]]\n", uniform.name, align, uniform.array_count); break;
Expand Down Expand Up @@ -306,17 +305,15 @@ static void write_shader_sources_and_blobs(const Input& inp,
}
if (0 == i) {
L("{:#04x}'u8,", blob->data[i]);
}
else {
} else {
L("{:#04x},", blob->data[i]);
}
if ((i & 15) == 15) {
L("\n");
}
}
L("\n]\n");
}
else {
} else {
// if no bytecode exists, write the source code, but also a byte array with a trailing 0
std::string nim_name = to_camel_case(fmt::format("{}_{}_source_{}", mod_prefix(inp), snippet.name, Slang::to_str(slang)));
const size_t len = src.source_code.length() + 1;
Expand All @@ -327,8 +324,7 @@ static void write_shader_sources_and_blobs(const Input& inp,
}
if (0 == i) {
L("{:#04x}'u8,", src.source_code[i]);
}
else {
} else {
L("{:#04x},", src.source_code[i]);
}
if ((i & 15) == 15) {
Expand All @@ -350,14 +346,12 @@ static void write_stage(const char* indent,
{
if (blob) {
L("{}result.{}.bytecode = {}\n", indent, stage_name, blob_name);
}
else {
} else {
L("{}result.{}.source = cast[cstring](addr({}))\n", indent, stage_name, src_name);
const char* d3d11_tgt = nullptr;
if (slang == Slang::HLSL4) {
d3d11_tgt = (0 == strcmp("vs", stage_name)) ? "vs_4_0" : "ps_4_0";
}
else if (slang == Slang::HLSL5) {
} else if (slang == Slang::HLSL5) {
d3d11_tgt = (0 == strcmp("vs", stage_name)) ? "vs_5_0" : "ps_5_0";
}
if (d3d11_tgt) {
Expand All @@ -376,8 +370,7 @@ static void write_stage(const char* indent,
L("{}result.{}.uniformBlocks[{}].uniforms[0].name = \"{}\"\n", indent, stage_name, ub_index, ub->struct_name);
L("{}result.{}.uniformBlocks[{}].uniforms[0].type = {}\n", indent, stage_name, ub_index, uniform_type_to_flattened_sokol_type_str(ub->uniforms[0].type));
L("{}result.{}.uniformBlocks[{}].uniforms[0].arrayCount = {}\n", indent, stage_name, ub_index, roundup(ub->size, 16) / 16);
}
else {
} else {
for (int u_index = 0; u_index < (int)ub->uniforms.size(); u_index++) {
const Uniform& u = ub->uniforms[u_index];
L("{}result.{}.uniformBlocks[{}].uniforms[{}].name = \"{}.{}\"\n", indent, stage_name, ub_index, u_index, ub->inst_name, u.name);
Expand Down Expand Up @@ -427,14 +420,12 @@ static void write_shader_desc_init(const char* indent, const Program& prog, cons
std::string vs_blob_name, fs_blob_name;
if (vs_blob) {
vs_blob_name = to_camel_case(fmt::format("{}_{}_bytecode_{}", mod_prefix(inp), prog.vs_name, Slang::to_str(slang)));
}
else {
} else {
vs_src_name = to_camel_case(fmt::format("{}{}_source_{}", mod_prefix(inp), prog.vs_name, Slang::to_str(slang)));
}
if (fs_blob) {
fs_blob_name = to_camel_case(fmt::format("{}_{}_bytecode_{}", mod_prefix(inp), prog.fs_name, Slang::to_str(slang)));
}
else {
} else {
fs_src_name = to_camel_case(fmt::format("{}_{}_source_{}", mod_prefix(inp), prog.fs_name, Slang::to_str(slang)));
}

Expand All @@ -444,8 +435,7 @@ static void write_shader_desc_init(const char* indent, const Program& prog, cons
if (attr.slot >= 0) {
if (Slang::is_glsl(slang)) {
L("{}result.attrs[{}].name = \"{}\"\n", indent, attr_index, attr.name);
}
else if (Slang::is_hlsl(slang)) {
} else if (Slang::is_hlsl(slang)) {
L("{}result.attrs[{}].semName = \"{}\"\n", indent, attr_index, attr.sem_name);
L("{}result.attrs[{}].semIndex = {}\n", indent, attr_index, attr.sem_index);
}
Expand Down
Loading

0 comments on commit f6684e6

Please # to comment.