Skip to content

Instantly share code, notes, and snippets.

@ruofeidu
Last active May 12, 2021 23:00
Show Gist options
  • Save ruofeidu/cc04ca88641d7dcaaa0f6cb8533adc1f to your computer and use it in GitHub Desktop.
Save ruofeidu/cc04ca88641d7dcaaa0f6cb8533adc1f to your computer and use it in GitHub Desktop.
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <fstream>
#include <string>
#include <numeric>
#include <algorithm>
#include <vector>
#include <set>
#include <sstream>
#include <random>
#include <cstdio>
#include <cmath>
#include "windows.h"
#include "dirent.h"
using namespace std;
string ExePath() {
char buffer[MAX_PATH];
GetModuleFileName(NULL, buffer, MAX_PATH);
string::size_type pos = string(buffer).find_last_of("\\/");
return string(buffer).substr(0, pos);
}
bool replace(std::string& str, const std::string& from, const std::string& to) {
size_t start_pos = str.find(from);
if (start_pos == std::string::npos)
return false;
str.replace(start_pos, from.length(), to);
return true;
}
bool replace_reverse(std::string& str, const std::string& from, const std::string& to) {
size_t start_pos = str.rfind(from);
if (start_pos == std::string::npos)
return false;
str.replace(start_pos, from.length(), to);
return true;
}
void replaceAll(std::string& str, const std::string& from, const std::string& to) {
if (from.empty()) return;
size_t start_pos = 0;
while ((start_pos = str.find(from, start_pos)) != std::string::npos) {
str.replace(start_pos, from.length(), to);
start_pos += to.length(); // In case 'to' contains 'from', like replacing 'x' with 'yx'
}
}
// find vec2(, 3(, 4( and fill the rest
void findAddFill(std::string& str, const std::string& from, int count) {
if (from.empty()) return;
size_t start_pos = 0;
while ((start_pos = str.find(from, start_pos)) != std::string::npos) {
size_t end_pos = str.find(")", start_pos);
string string_to_insert = "";
// if find correspondence
if (end_pos != string::npos) {
size_t comma_pos = str.substr(start_pos, end_pos - start_pos).find(","); // str.find("\,", start_pos, end_pos - start_pos);
if (comma_pos == string::npos) {
auto number_str = str.substr(start_pos + from.size(), end_pos - start_pos - from.size());
for (auto c = 0; c < count; ++c) {
string_to_insert += ", " + number_str;
}
str.insert(end_pos, string_to_insert);
}
}
start_pos += string_to_insert.length() + 1; // In case 'to' contains 'from', like replacing 'x' with 'yx'
}
}
int main() {
ios::fmtflags cin_screen(cin.flags());
ios::fmtflags cout_screen(cout.flags());
string exe_path = ExePath();
cout << "Current directory is " << exe_path << "\n";
DIR *dir;
struct dirent *ent;
string read_path = exe_path;
set<string> set_files;
if ((dir = opendir(read_path.c_str())) != NULL) {
while ((ent = readdir(dir)) != NULL) {
string file = string(ent->d_name);
string prefix = file.substr(0, file.find("."));
string suffix = file.substr(file.find_last_of("."));
// find all files ended with .glsl
if (!suffix.compare(".glsl")) {
set_files.insert(prefix);
printf("* %s\n", prefix.c_str());
string input_file = read_path + "\\" + prefix + ".glsl";
string output_file = read_path + "\\" + prefix + ".hlsl";
/*freopen(input_file.c_str(), "r", stdin);
freopen(output_file.c_str(), "w", stdout);*/
//string text = "";
//string line;
//while (getline(cin, line))
//{
// text += line + "\n";
//}
ifstream ifs(input_file);
string text((istreambuf_iterator<char>(ifs)), (istreambuf_iterator<char>()));
ofstream ofs(output_file);
replace(text, "void mainImage( out vec4 fragColor, in vec2 fragCoord ) {", "float4 frag(v2f vert) : SV_Target {");
replace(text, "void mainImage( out vec4 fragColor, in vec2 fragCoord )", "float4 frag(v2f vert) : SV_Target");
findAddFill(text, "vec4(", 3);
findAddFill(text, "vec3(", 2);
findAddFill(text, "vec2(", 1);
replaceAll(text, "ivec4", "int4");
replaceAll(text, "ivec3", "int3");
replaceAll(text, "ivec2", "int2");
replaceAll(text, "vec4", "float4");
replaceAll(text, "vec3", "float3");
replaceAll(text, "vec2", "float2");
replaceAll(text, "mat4", "float4x4");
replaceAll(text, "mat3", "float3x3");
replaceAll(text, "mat2", "float2x2");
replaceAll(text, "iResolution", "_ScreenParams");
replaceAll(text, "iGlobalTime", "_Time.y");
replaceAll(text, "iTime", "_Time.y");
replaceAll(text, "fragCoord", "(vert.uv.xy * _ScreenParams.xy)");
replaceAll(text, "iChannel0", "_MainTex");
replaceAll(text, "texture(", "tex2D(");
replaceAll(text, "texture2D(", "tex2D(");
replaceAll(text, "mod(", "fmod(");
replaceAll(text, "fract(", "frac(");
replaceAll(text, "mix(", "lerp(");
replaceAll(text, "inversesqrt(", "rsqrt(");
replace_reverse(text, "fragColor =", "return");
ofs << text << endl;
ifs.close();
ofs.close();
}
}
closedir(dir);
}
else {
perror("");
return EXIT_FAILURE;
}
cin.flags(cin_screen);
cout.flags(cout_screen);
system("pause");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment