-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathShadertoy.cpp
539 lines (454 loc) · 15.4 KB
/
Shadertoy.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
#include "Shadertoy.h"
#include "APIKey.h"
#include <imgui/imgui.h>
#include <imgui/imgui_internal.h>
#define CPPHTTPLIB_OPENSSL_SUPPORT
#include <httplib/httplib.h>
#include <json11/json11.hpp>
#include <pugixml/src/pugixml.hpp>
#include <ghc/filesystem.hpp>
#define BUTTON_SPACE_LEFT -40 * GetDPI()
#define KEYBOARD_TEXTURE_NAME "KeyboardTexture"
namespace st
{
struct ShaderOutput
{
int ID;
int Channel;
};
struct ShaderInputSampler
{
std::string Filter;
std::string Wrap;
bool FlipVertical;
bool SRGB;
};
struct ShaderInput
{
int ID;
int Channel;
std::string Type;
std::string Source;
ShaderInputSampler Sampler;
};
struct RenderPass
{
std::vector<ShaderOutput> Outputs;
std::vector<ShaderInput> Inputs;
std::string Name;
std::string Type;
std::string Code;
};
std::string GenerateReadMe(const json11::Json& info)
{
std::string ret = "";
ret += "Name: " + info["name"].string_value() + "\n";
ret += "Shader made by: " + info["username"].string_value() + "\n";
ret += "Link: www.shadertoy.com/view/" + info["id"].string_value() + "\n";
return ret;
}
std::vector<ShaderOutput> ParseOutputs(const json11::Json& outputs)
{
std::vector<ShaderOutput> ret;
if (outputs.is_array()) {
for (const auto& output : outputs.array_items()) {
ShaderOutput data;
data.Channel = output["channel"].int_value();
data.ID = output["id"].int_value();
ret.push_back(data);
}
}
return ret;
}
std::vector<ShaderInput> ParseInputs(const json11::Json& outputs)
{
std::vector<ShaderInput> ret;
if (outputs.is_array()) {
for (const auto& output : outputs.array_items()) {
ShaderInput data;
data.Channel = output["channel"].int_value();
data.ID = output["id"].int_value();
data.Source = output["src"].string_value();
data.Type = output["ctype"].string_value();
data.Sampler.Filter = output["sampler"]["filter"].string_value();
data.Sampler.Wrap = output["sampler"]["wrap"].string_value();
data.Sampler.FlipVertical = output["sampler"]["vflip"].bool_value();
data.Sampler.SRGB = output["sampler"]["srgb"].bool_value();
ret.push_back(data);
}
}
return ret;
}
std::vector<RenderPass> ParseRenderPasses(const json11::Json& rpassContainer)
{
std::vector<RenderPass> ret;
if (rpassContainer.is_array()) {
for (const auto& rpass : rpassContainer.array_items()) {
RenderPass data;
data.Inputs = ParseInputs(rpass["inputs"]);
data.Outputs = ParseOutputs(rpass["outputs"]);
data.Name = rpass["name"].string_value();
data.Type = rpass["type"].string_value();
data.Code = rpass["code"].string_value();
ret.push_back(data);
}
}
return ret;
}
std::string GenerateItems(int index)
{
std::string ret =
"<items>\n"
"<item name=\"ScreenQuad" + std::to_string(index) + "\" type=\"geometry\">\n"
"<type>ScreenQuadNDC</type>\n"
"<width>1</width>\n"
"<height>1</height>\n"
"<depth>1</depth>\n"
"<topology>TriangleList</topology>\n"
"</item>\n"
"</items>";
return ret;
}
std::string GenerateVariables()
{
std::string ret =
"<variables>"
"<variable type=\"float2\" name=\"iResolution\" system=\"ViewportSize\" />"
"<variable type=\"float\" name=\"iTime\" system=\"Time\" />"
"<variable type=\"float\" name=\"iTimeDelta\" system=\"TimeDelta\" />"
"<variable type=\"int\" name=\"iFrame\" system=\"FrameIndex\" />"
"<variable type=\"float4\" name=\"iMouse\" system=\"MouseButton\" />"
"</variables>";
return ret;
}
std::string GenerateSettings()
{
std::string ret =
"<entry type=\"camera\" fp=\"false\">"
"<distance>10</distance>"
"<pitch>0</pitch>"
"<yaw>0</yaw>"
"<roll>0</roll>"
"</entry>"
"<entry type=\"clearcolor\" r=\"0\" g=\"0\" b=\"0\" a=\"0\" />"
"<entry type=\"usealpha\" val=\"false\" />";
return ret;
}
std::string GenerateVertexShader()
{
const char* vs = R"(#version 330
layout (location = 0) in vec2 pos;
layout (location = 1) in vec2 uv;
out vec2 outUV;
void main() {
gl_Position = vec4(pos, 0.0, 1.0);
outUV = uv;
}
)";
return std::string(vs);
}
std::string GenerateGLSL(const std::string& code, bool usesCommon = false)
{
std::string ret = "#version 330\n\n" +
std::string(usesCommon ? "#include <common.glsl>\n" : "") +
"uniform vec2 iResolution;\n"
"uniform float iTime;\n"
"uniform float iTimeDelta;\n"
"uniform int iFrame;\n"
"uniform vec4 iMouse;\n"
"uniform sampler2D iChannel0;\n"
"uniform sampler2D iChannel1;\n"
"uniform sampler2D iChannel2;\n"
"uniform sampler2D iChannel3;\n"
"out vec4 shadertoy_outcolor;\n\n" + code + "\n"
"void main()\n{\n"
"\tmainImage(shadertoy_outcolor, gl_FragCoord.xy);\n"
"}";
return ret;
}
pugi::xml_document GenerateProject(const std::vector<RenderPass>& data)
{
pugi::xml_document doc;
pugi::xml_node project = doc.append_child("project");
project.append_attribute("version").set_value(2);
pugi::xml_node pipelineNode = project.append_child("pipeline");
pugi::xml_node objectsNode = project.append_child("objects");
pugi::xml_node settingsNode = project.append_child("settings");
/////// BUILD RESOURCE LIST ///////
int index = 0;
std::vector<std::string> rts;
std::vector<int> rtIds;
std::map<int, std::vector<std::pair<std::string, int>>> rtBind;
std::vector<std::string> textures, textureTypes;
std::map<std::string, std::vector<std::pair<std::string, int>>> texBinds;
for (const auto& rpass : data) {
if (rpass.Type == "buffer") {
rts.push_back(rpass.Name);
rtIds.push_back(rpass.Outputs[0].ID);
}
for (const auto& inp : rpass.Inputs) {
if (inp.Type == "texture" || inp.Type == "keyboard") {
std::string name = inp.Source;
if (inp.Type == "keyboard")
name = KEYBOARD_TEXTURE_NAME;
if (std::count(textures.begin(), textures.end(), name) == 0)
textures.push_back(name);
texBinds[name].push_back(std::make_pair(rpass.Name, inp.Channel));
}
else if (inp.Type == "buffer")
rtBind[inp.ID].push_back(std::make_pair(rpass.Name, inp.Channel));
}
index++;
}
/////// PIPELINE ///////
for (int i = data.size() - 1; i >= 0; i--) {
const RenderPass& pass = data[i];
if (pass.Type == "common")
continue;
pugi::xml_node node = pipelineNode.append_child("pass");
node.append_attribute("name").set_value(pass.Name.c_str());
node.append_attribute("type").set_value("shader");
node.append_attribute("active").set_value("true");
pugi::xml_node vsNode = node.append_child("shader");
vsNode.append_attribute("type").set_value("vs");
vsNode.append_attribute("path").set_value("shaders/shadertoyVS.glsl");
pugi::xml_node psNode = node.append_child("shader");
psNode.append_attribute("type").set_value("ps");
psNode.append_attribute("path").set_value(("shaders/" + pass.Name + ".glsl").c_str());
if (pass.Type == "buffer")
node.append_child("rendertexture").append_attribute("name").set_value(pass.Name.c_str());
else
node.append_child("rendertexture");
std::string itemsNode = GenerateItems(data.size() - i);
node.append_buffer(itemsNode.c_str(), itemsNode.size());
std::string varNode = GenerateVariables();
node.append_buffer(varNode.c_str(), varNode.size());
}
/////// OBJECTS ///////
for (int i = 0; i < rts.size(); i++) {
pugi::xml_node node = objectsNode.append_child("object");
node.append_attribute("type").set_value("rendertexture");
node.append_attribute("name").set_value(rts[i].c_str());
node.append_attribute("rsize").set_value("1.00,1.00");
node.append_attribute("clear").set_value("true");
node.append_attribute("r").set_value("0");
node.append_attribute("g").set_value("0");
node.append_attribute("b").set_value("0");
node.append_attribute("a").set_value("1");
const std::vector<std::pair<std::string, int>>& myBind = rtBind[rtIds[i]];
for (int j = 0; j < myBind.size(); j++) {
auto& pair = myBind[j];
pugi::xml_node bindNode = node.append_child("bind");
bindNode.append_attribute("slot").set_value(pair.second);
bindNode.append_attribute("name").set_value(pair.first.c_str());
}
}
for (int i = 0; i < textures.size(); i++) {
pugi::xml_node node = objectsNode.append_child("object");
node.append_attribute("type").set_value("texture");
if (textures[i] == KEYBOARD_TEXTURE_NAME) {
node.append_attribute("name").set_value(textures[i].c_str());
node.append_attribute("keyboard_texture").set_value(true);
} else {
node.append_attribute("path").set_value(("." + textures[i]).c_str());
ShaderInputSampler samplerInfo;
for (int j = 0; j < data.size(); j++)
for (int k = 0; k < data[j].Inputs.size(); k++) {
if (data[j].Inputs[k].Source == textures[i])
samplerInfo = data[j].Inputs[k].Sampler;
}
// vertical flip
node.append_attribute("vflip").set_value(samplerInfo.FlipVertical);
// filter
if (samplerInfo.Filter == "linear") {
node.append_attribute("min_filter").set_value("Nearest");
node.append_attribute("mag_filter").set_value("Nearest");
} else if (samplerInfo.Filter == "nearest") {
node.append_attribute("min_filter").set_value("Linear");
node.append_attribute("mag_filter").set_value("Linear");
} else if (samplerInfo.Filter == "mipmap") {
/* TODO: not sure what this is supposed to be */
node.append_attribute("min_filter").set_value("Linear_MipmapLinear");
node.append_attribute("mag_filter").set_value("Linear");
}
// wrap
if (samplerInfo.Wrap == "clamp") {
node.append_attribute("wrap_s").set_value("ClampToEdge");
node.append_attribute("wrap_t").set_value("ClampToEdge");
} else if (samplerInfo.Wrap == "repeat") {
node.append_attribute("wrap_s").set_value("Repeat");
node.append_attribute("wrap_t").set_value("Repeat");
}
}
const std::vector<std::pair<std::string, int>>& myBind = texBinds[textures[i]];
for (int j = 0; j < myBind.size(); j++) {
auto& pair = myBind[j];
pugi::xml_node bindNode = node.append_child("bind");
bindNode.append_attribute("slot").set_value(pair.second);
bindNode.append_attribute("name").set_value(pair.first.c_str());
}
}
/////// SETTINGS ///////
std::string settings = GenerateSettings();
settingsNode.append_buffer(settings.c_str(), settings.size());
return doc;
}
void WriteFile(const std::string& filename, const std::string& filedata)
{
std::ofstream file(filename);
file << filedata;
file.close();
}
bool Generate(const std::string& shadertoyID, const std::string& outPath)
{
// https://www.shadertoy.com/api/v1/shaders/shaderID?key=appkey
httplib::SSLClient cli("www.shadertoy.com");
auto res = cli.Get(("/api/v1/shaders/" + shadertoyID + "?key=" SHADERTOY_APIKEY).c_str());
std::vector<RenderPass> pipeline;
if (res && res->status == 200) {
std::string err;
json11::Json jdata = json11::Json::parse(res->body, err);
if (jdata["Error"].is_string()) {
return false;
}
if (jdata.is_object()) {
pipeline = ParseRenderPasses(jdata["Shader"]["renderpass"]);
}
if (!ghc::filesystem::exists(outPath))
ghc::filesystem::create_directories(outPath);
std::string shadersDir = outPath + "/shaders";
if (!ghc::filesystem::exists(shadersDir))
ghc::filesystem::create_directories(shadersDir);
// README.txt
WriteFile(outPath + "/README.txt", GenerateReadMe(jdata["Shader"]["info"]));
// project.sprj
pugi::xml_document doc = GenerateProject(pipeline);
std::ofstream sprjFile(outPath + "/project.sprj");
doc.print(sprjFile);
sprjFile.close();
// shaders
bool usesCommon = false;
for (const auto& item : pipeline) {
if (item.Type == "common") {
usesCommon = true;
WriteFile(outPath + "/common.glsl", item.Code);
break;
}
}
for (const auto& item : pipeline) {
if (item.Type == "common")
continue;
std::string shaderPath = outPath + "/shaders/" + item.Name + ".glsl";
WriteFile(shaderPath, GenerateGLSL(item.Code, usesCommon));
}
WriteFile(outPath + "/shaders/shadertoyVS.glsl", GenerateVertexShader());
// textures
std::vector<std::string> exportedTexs;
for (const auto& rpass : pipeline) {
for (const auto& inp : rpass.Inputs) {
if (inp.Type == "texture") {
if (std::count(exportedTexs.begin(), exportedTexs.end(), inp.Source) > 0)
continue;
exportedTexs.push_back(inp.Source);
std::string texPath = outPath + inp.Source;
if (!ghc::filesystem::exists(texPath))
ghc::filesystem::create_directories(ghc::filesystem::path(texPath).parent_path());
std::ofstream texFile(texPath, std::ofstream::binary);
auto res = cli.Get(inp.Source.c_str());
if (res && res->status == 200)
texFile.write(res->body.c_str(), res->body.size());
texFile.close();
}
}
}
return err.size() == 0;
}
return false;
}
bool Shadertoy::Init(bool isWeb, int sedVersion) {
m_isPopupOpened = false;
if (sedVersion == 1003005)
m_hostVersion = 1;
else
m_hostVersion = GetHostIPluginMaxVersion();
return true;
}
void Shadertoy::InitUI(void* ctx)
{
ImGui::SetCurrentContext((ImGuiContext*)ctx);
}
void Shadertoy::Update(float delta)
{
// ##### UNIFORM MANAGER POPUP #####
if (m_isPopupOpened) {
ImGui::OpenPopup("Import Shadertoy project##st_import");
m_error = "";
m_isPopupOpened = false;
}
ImGui::SetNextWindowSize(ImVec2(530, 160), ImGuiCond_Once);
if (ImGui::BeginPopupModal("Import Shadertoy project##st_import")) {
ImGui::Text("Shadertoy link:"); ImGui::SameLine();
ImGui::PushItemWidth(-1);
ImGui::InputText("##st_link_insert", m_link, 256);
ImGui::PopItemWidth();
ImGui::Text("Project path:"); ImGui::SameLine();
ImGui::PushItemWidth(BUTTON_SPACE_LEFT);
ImGui::PushItemFlag(ImGuiItemFlags_Disabled, true);
ImGui::InputText("##pui_vspath", m_path, MY_PATH_LENGTH);
ImGui::PopItemFlag();
ImGui::PopItemWidth();
ImGui::SameLine();
if (ImGui::Button("...##pui_vsbtn", ImVec2(-1, 0)) && m_hostVersion >= 2)
ImGuiDirectoryDialogOpen("ShadertoyLocationDlg", "Save location");
if (m_hostVersion >= 2 && ImGuiFileDialogIsDone("ShadertoyLocationDlg")) {
if (ImGuiFileDialogGetResult())
ImGuiFileDialogGetPath(m_path);
ImGuiFileDialogClose("ShadertoyLocationDlg");
}
if (!m_errorOccured)
ImGui::NewLine();
else
ImGui::Text("[ERROR] %s", m_error.c_str());
if (ImGui::Button("Ok")) {
std::string stLink = m_link;
std::string errMessage = "";
if (stLink.find("www.shadertoy.com/view/") == std::string::npos)
errMessage = "Please insert correct Shadertoy link.";
if (errMessage.size() == 0) {
size_t lastSlash = stLink.find_last_of('/');
std::string id = stLink.substr(lastSlash+1);
std::string outPath(m_path);
if (outPath.size() == 0)
errMessage = "Please set the output path";
else {
bool res = Generate(id, outPath);
if (!res)
errMessage = "Shader either doesn't exist or doesn't have the PublicAPI flag set";
else
OpenProject(UI, (outPath + "/project.sprj").c_str());
}
}
m_error = errMessage;
m_errorOccured = (m_error.size() != 0);
if (m_error.size() == 0)
ImGui::CloseCurrentPopup();
}
ImGui::SameLine();
if (ImGui::Button("Cancel"))
ImGui::CloseCurrentPopup();
ImGui::EndPopup();
}
}
bool Shadertoy::HasMenuItems(const char* name)
{
return strcmp(name, "file") == 0;
}
void Shadertoy::ShowMenuItems(const char* name)
{
if (strcmp(name, "file") == 0) {
if (ImGui::Selectable("Import Shadertoy project")) {
m_isPopupOpened = true;
}
}
}
}