5d83c11169
- cpp/functions/gfx/dag_types: DagStep, DagNodeDef, DagControl (header-only) - cpp/functions/gfx/dag_catalog: 10 hardcoded nodes (4 gen, 3 op, 3 blend) ported from shader-dag-blends.jsx - cpp/functions/gfx/dag_compile: pipeline → GLSL 330 core with fan-in via source_id - cpp/functions/gfx/dag_uniforms: upload u_params[16] via glUniform4fv - cpp/functions/gfx/dag_panel: ImGui pipeline editor (add/remove/reorder/controls) - main.cpp: Code/DAG mode toggle, per-mode compile path and uniforms - gl_loader: +glUniform4fv - rebuild Windows .exe Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
85 lines
2.6 KiB
C++
85 lines
2.6 KiB
C++
#include "gfx/dag_compile.h"
|
|
#include "gfx/dag_catalog.h"
|
|
#include <algorithm>
|
|
#include <sstream>
|
|
|
|
namespace fn::gfx {
|
|
|
|
static constexpr int MAX_NODES = 16;
|
|
|
|
std::string compile_dag_to_glsl(const std::vector<DagStep>& pipeline) {
|
|
const int n = static_cast<int>(std::min(pipeline.size(), static_cast<size_t>(MAX_NODES)));
|
|
std::ostringstream out;
|
|
|
|
out << "uniform vec4 u_params[16];\n\n";
|
|
|
|
if (n == 0) {
|
|
out << "void main() {\n";
|
|
out << " vec2 uv = gl_FragCoord.xy / u_resolution;\n";
|
|
out << " (void)uv;\n";
|
|
out << " fragColor = vec4(0.04, 0.04, 0.06, 1.0);\n";
|
|
out << "}\n";
|
|
return out.str();
|
|
}
|
|
|
|
for (int i = 0; i < n; ++i) {
|
|
const DagStep& step = pipeline[static_cast<size_t>(i)];
|
|
const DagNodeDef* def = dag_find(step.name);
|
|
if (!def) continue;
|
|
|
|
if (def->kind == DagKind::Blend) {
|
|
out << "vec4 node_" << i << "(vec4 a, vec4 b, vec2 uv) {\n";
|
|
} else {
|
|
out << "vec4 node_" << i << "(vec4 c, vec2 uv) {\n";
|
|
}
|
|
out << def->body_glsl(i) << "\n";
|
|
out << "}\n\n";
|
|
}
|
|
|
|
out << "void main() {\n";
|
|
out << " vec2 uv = gl_FragCoord.xy / u_resolution;\n";
|
|
out << " vec4 c = vec4(0.04, 0.04, 0.06, 1.0);\n";
|
|
|
|
for (int i = 0; i < n; ++i) {
|
|
const DagStep& step = pipeline[static_cast<size_t>(i)];
|
|
const DagNodeDef* def = dag_find(step.name);
|
|
if (!def) {
|
|
out << " vec4 out_" << i << " = (i > 0 ? out_" << (i-1) << " : c);\n";
|
|
continue;
|
|
}
|
|
|
|
std::string prev = (i == 0) ? "vec4(0.0, 0.0, 0.0, 1.0)" : "out_" + std::to_string(i - 1);
|
|
|
|
if (def->kind == DagKind::Blend) {
|
|
int src_idx = -1;
|
|
if (!step.source_id.empty()) {
|
|
for (int j = 0; j < i; ++j) {
|
|
if (pipeline[static_cast<size_t>(j)].id == step.source_id) {
|
|
src_idx = j;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
if (src_idx < 0 || src_idx >= i) {
|
|
src_idx = std::max(0, i - 2);
|
|
}
|
|
std::string src;
|
|
if (i == 0) {
|
|
src = prev;
|
|
} else {
|
|
src = "out_" + std::to_string(std::min(src_idx, i - 1));
|
|
}
|
|
out << " vec4 out_" << i << " = node_" << i << "(" << prev << ", " << src << ", uv);\n";
|
|
} else {
|
|
out << " vec4 out_" << i << " = node_" << i << "(" << prev << ", uv);\n";
|
|
}
|
|
}
|
|
|
|
out << " fragColor = out_" << (n - 1) << ";\n";
|
|
out << "}\n";
|
|
|
|
return out.str();
|
|
}
|
|
|
|
} // namespace fn::gfx
|