Kemena3D
Loading...
Searching...
No Matches
kshadernode.h
Go to the documentation of this file.
1#pragma once
2#include <string>
3#include <vector>
4#include <map>
5#include <set>
6#include <unordered_map>
7#include "kdatatype.h"
8#include "nlohmann/json.hpp"
9
10namespace kemena
11{
12
13// ---------------------------------------------------------------------------
14// Pin / connection types
15// ---------------------------------------------------------------------------
16
32
38inline const char* kPinTypeName(kPinType t)
39{
40 switch (t)
41 {
42 case kPinType::Float: return "float";
43 case kPinType::Vec2: return "vec2";
44 case kPinType::Vec3: return "vec3";
45 case kPinType::Vec4: return "vec4";
46 case kPinType::Sampler2D: return "sampler2D";
47 case kPinType::SamplerCube: return "samplerCube";
48 }
49 return "float";
50}
51
62inline bool kPinCompatible(kPinType from, kPinType to)
63{
64 if (from == to) return true;
65 // float → any numeric
66 if (from == kPinType::Float &&
67 (to == kPinType::Vec2 || to == kPinType::Vec3 || to == kPinType::Vec4))
68 return true;
69 // vec3 → vec4
70 if (from == kPinType::Vec3 && to == kPinType::Vec4) return true;
71 // vec4 → vec3 (take .rgb)
72 if (from == kPinType::Vec4 && to == kPinType::Vec3) return true;
73 return false;
74}
75
76// ---------------------------------------------------------------------------
77// Shader node types (kShaderNodeType to avoid clash with kNodeType in kdatatype.h)
78// ---------------------------------------------------------------------------
79
148
149// ---------------------------------------------------------------------------
150// Pin descriptor
151// ---------------------------------------------------------------------------
152
160{
161 int id = 0;
164 bool isOutput = false;
165
166 // Default value when no link feeds this pin
167 float defFloat = 0.0f;
168 float defVec[4] = { 0.f, 0.f, 0.f, 1.f };
169
170 // Runtime UI position (screen space, set during draw)
171 float uiX = 0, uiY = 0;
172};
173
174// ---------------------------------------------------------------------------
175// Node
176// ---------------------------------------------------------------------------
177
185{
186 int id = 0;
189 float posX = 200.f, posY = 200.f;
190
191 std::vector<kShaderPin> inputs;
192 std::vector<kShaderPin> outputs;
193
194 // Node-specific payload
195 float valueFloat[4] = { 0.f, 0.f, 0.f, 1.f };
197 bool valueBool = false;
198
204 static const char* typeName(kShaderNodeType t);
205};
206
207// ---------------------------------------------------------------------------
208// Link (directed edge: output pin → input pin)
209// ---------------------------------------------------------------------------
210
215{
216 int id = 0;
217 int fromNode = 0, fromPin = 0;
218 int toNode = 0, toPin = 0;
219};
220
221// ---------------------------------------------------------------------------
222// Graph
223// ---------------------------------------------------------------------------
224
232{
235 std::vector<kShaderNode> nodes;
236 std::vector<kShaderLink> links;
237 int nextId = 1;
238 bool dirty = false;
239
244 int newId() { return nextId++; }
245
252
258 const kShaderNode* findNode(int id) const;
259
266 const kShaderLink* incomingLink(int nodeId, int pinId) const;
267
274 bool isPinConnected(int nodeId, int pinId) const;
275
280 void removeLinksByNode(int nodeId);
281
287 void removeLinksByPin(int nodeId, int pinId);
288
296 kShaderNode makeNode(kShaderNodeType type, float x, float y);
297
302 nlohmann::json toJson() const;
303
308 void fromJson(const nlohmann::json& j);
309};
310
311// ---------------------------------------------------------------------------
312// Compiler: walks the graph and emits combined GLSL source
313// ---------------------------------------------------------------------------
314
322{
323 bool success = false;
326
327 // Auto-detected material inputs (for material inspector display)
328 bool usesAlbedoMap = false;
329 bool usesNormalMap = false;
331 bool usesAoMap = false;
332 bool usesEmissiveMap = false;
333 std::vector<std::pair<kString, kString>> customSamplers;
334 bool needsMaterial = false;
335 bool needsLights = false;
336};
337
345{
346public:
353
354private:
361 struct Ctx
362 {
363 std::map<int, kString> nodeVar;
364 std::vector<kString> samplers;
365 std::vector<kString> uniforms;
366 std::set<kString> emittedSamplerNames;
367 std::vector<kString> body;
368 int counter = 0;
369
374 kString newVar() { return "v_" + std::to_string(counter++); }
375 };
376
385 static kString emitPin(const kShaderGraph& g, int nodeId, int pinId, Ctx& ctx);
386
394 static kString emitNode(const kShaderGraph& g, int nodeId, Ctx& ctx);
395
403 static kString promote(const kString& expr, kPinType actual, kPinType target);
404
410 static kString pinDefault(const kShaderPin& pin);
411
417 static kString glslType(kPinType t) { return kPinTypeName(t); }
418
423 static kString vertexTemplate();
424
432 static kString fragmentPreamble(const kShaderGraph& g, const kShaderNode& outNode,
433 const Ctx& ctx);
434
440 static kString lightingCode(kShaderNodeType outType);
441};
442
443} // namespace kemena
Compiles a kShaderGraph into combined GLSL source.
Definition kshadernode.h:345
static kShaderCompileResult compile(const kShaderGraph &graph)
Compiles a shader graph into GLSL.
Core type aliases, enumerations, structs, and utility functions used throughout the engine.
Top-level Kemena3D engine namespace.
Definition kanimation.h:23
@ Divide
a / b.
Definition kscriptgraph.h:93
@ Subtract
a - b.
Definition kscriptgraph.h:91
@ Multiply
a * b.
Definition kscriptgraph.h:92
@ Add
a + b.
Definition kscriptgraph.h:90
kPinType
Data type carried by a shader-node pin (link/connection type).
Definition kshadernode.h:24
@ Vec3
3-component vector.
Definition kshadernode.h:27
@ Float
Scalar float.
Definition kshadernode.h:25
@ SamplerCube
Cube-map texture sampler.
Definition kshadernode.h:30
@ Sampler2D
2D texture sampler.
Definition kshadernode.h:29
@ Vec4
4-component vector.
Definition kshadernode.h:28
@ Vec2
2-component vector.
Definition kshadernode.h:26
bool kPinCompatible(kPinType from, kPinType to)
Tests whether an output pin type may feed into an input pin type.
Definition kshadernode.h:62
@ Vec3
3-component vector value.
Definition kscriptgraph.h:42
@ Float
Floating-point value.
Definition kscriptgraph.h:39
std::string kString
Standard string alias.
Definition kdatatype.h:42
const char * kPinTypeName(kPinType t)
Returns the GLSL type keyword for a pin type.
Definition kshadernode.h:38
kShaderNodeType
Identifies the kind of operation a shader graph node performs.
Definition kshadernode.h:89
@ WorldPosition
Fragment world-space position.
Definition kshadernode.h:98
@ Swizzle
Reorder/select components via a mask.
Definition kshadernode.h:132
@ Clamp
Clamp value between min and max.
Definition kshadernode.h:115
@ TextureCube
Sample a cube-map texture.
Definition kshadernode.h:104
@ ConstVec3
User-supplied constant vec3.
Definition kshadernode.h:93
@ OutputPhong
Phong lit output (requires lights).
Definition kshadernode.h:145
@ Abs
Absolute value.
Definition kshadernode.h:118
@ Smoothstep
Smooth Hermite interpolation between edges.
Definition kshadernode.h:126
@ Mix
Linear interpolation (lerp).
Definition kshadernode.h:116
@ MaterialDiffuse
Material diffuse color.
Definition kshadernode.h:137
@ ViewDirection
Direction from fragment toward the camera.
Definition kshadernode.h:99
@ Fract
Fractional part.
Definition kshadernode.h:121
@ Step
Step function (0 or 1 about an edge).
Definition kshadernode.h:125
@ OutputFlat
Unlit/flat shading output.
Definition kshadernode.h:144
@ UVCoord
Interpolated texture coordinates.
Definition kshadernode.h:95
@ Max
Component-wise maximum.
Definition kshadernode.h:124
@ OneMinus
1 - value.
Definition kshadernode.h:127
@ Min
Component-wise minimum.
Definition kshadernode.h:123
@ Split
Decompose a vector into its components.
Definition kshadernode.h:130
@ Pow
Raise base to an exponent.
Definition kshadernode.h:117
@ ConstVec4
User-supplied constant vec4.
Definition kshadernode.h:94
@ MaterialTiling
Material UV tiling factor.
Definition kshadernode.h:135
@ MaterialSpecular
Material specular color.
Definition kshadernode.h:138
@ MaterialAmbient
Material ambient color.
Definition kshadernode.h:136
@ ConstVec2
User-supplied constant vec2.
Definition kshadernode.h:92
@ Texture2D
Sample a 2D texture.
Definition kshadernode.h:103
@ Time
Elapsed time uniform.
Definition kshadernode.h:96
@ Sqrt
Square root.
Definition kshadernode.h:122
@ Dot
Dot product.
Definition kshadernode.h:111
@ MaterialMetallic
Material metallic factor (PBR).
Definition kshadernode.h:140
@ Ceil
Round up to integer.
Definition kshadernode.h:120
@ Length
Vector length.
Definition kshadernode.h:114
@ ConstFloat
User-supplied constant float.
Definition kshadernode.h:91
@ MaterialShininess
Material shininess exponent.
Definition kshadernode.h:139
@ VertexColor
Per-vertex color attribute.
Definition kshadernode.h:97
@ OutputPBR
Physically based output (requires lights).
Definition kshadernode.h:146
@ Normalize
Normalize a vector.
Definition kshadernode.h:113
@ MaterialRoughness
Material roughness factor (PBR).
Definition kshadernode.h:141
@ VertexNormal
Interpolated surface normal.
Definition kshadernode.h:100
@ Cross
Cross product.
Definition kshadernode.h:112
@ Combine
Assemble scalars into a vector.
Definition kshadernode.h:131
@ Floor
Round down to integer.
Definition kshadernode.h:119
Result of compiling a shader graph into GLSL.
Definition kshadernode.h:322
bool needsMaterial
Graph references material.* uniforms.
Definition kshadernode.h:334
bool usesAoMap
Graph samples an ambient-occlusion map.
Definition kshadernode.h:331
bool usesMetallicRoughnessMap
Graph samples a metallic-roughness map.
Definition kshadernode.h:330
kString error
Error message when success is false.
Definition kshadernode.h:324
bool usesAlbedoMap
Graph samples an albedo/base-color map.
Definition kshadernode.h:328
kString glsl
Combined vertex + fragment GLSL source.
Definition kshadernode.h:325
bool usesEmissiveMap
Graph samples an emissive map.
Definition kshadernode.h:332
bool success
True if compilation succeeded.
Definition kshadernode.h:323
bool usesNormalMap
Graph samples a normal map.
Definition kshadernode.h:329
std::vector< std::pair< kString, kString > > customSamplers
Extra samplers: { uniformName, "2D"|"Cube" }.
Definition kshadernode.h:333
bool needsLights
Output requires lighting (Phong/PBR).
Definition kshadernode.h:335
A complete node-based shader graph: nodes, links, and identity.
Definition kshadernode.h:232
int newId()
Allocates and returns a fresh unique id.
Definition kshadernode.h:244
void fromJson(const nlohmann::json &j)
Rebuilds the graph from a JSON representation.
bool isPinConnected(int nodeId, int pinId) const
Tests whether a pin has any link attached.
nlohmann::json toJson() const
Serializes the graph to JSON.
void removeLinksByPin(int nodeId, int pinId)
Removes every link touching the given pin.
const kShaderNode * findNode(int id) const
Finds a node by id (const overload).
bool dirty
True when the graph has unsaved changes.
Definition kshadernode.h:238
kString name
Display name of the graph.
Definition kshadernode.h:234
const kShaderLink * incomingLink(int nodeId, int pinId) const
Finds the link feeding a given input pin.
std::vector< kShaderNode > nodes
All nodes in the graph.
Definition kshadernode.h:235
void removeLinksByNode(int nodeId)
Removes every link touching the given node.
int nextId
Next id to hand out via newId().
Definition kshadernode.h:237
kShaderNode makeNode(kShaderNodeType type, float x, float y)
Creates a node of a given type with its default pins wired up.
std::vector< kShaderLink > links
All links between node pins.
Definition kshadernode.h:236
kString uuid
Stable unique identifier for the graph asset.
Definition kshadernode.h:233
kShaderNode * findNode(int id)
Finds a node by id.
A node in the shader graph: a typed operation with input/output pins.
Definition kshadernode.h:185
float valueFloat[4]
Constant value for Const* nodes.
Definition kshadernode.h:195
kString name
Display name of the node.
Definition kshadernode.h:188
kShaderNodeType type
Operation this node performs.
Definition kshadernode.h:187
std::vector< kShaderPin > outputs
Output pins.
Definition kshadernode.h:192
float posY
Node position in the editor canvas.
Definition kshadernode.h:189
kString valueStr
Texture uniform name or swizzle mask.
Definition kshadernode.h:196
std::vector< kShaderPin > inputs
Input pins.
Definition kshadernode.h:191
static const char * typeName(kShaderNodeType t)
Returns a human-readable name for a shader node type.
float posX
Definition kshadernode.h:189
bool valueBool
Generic boolean payload.
Definition kshadernode.h:197
A single input or output connection point on a shader node.
Definition kshadernode.h:160
kPinType type
Data type carried by the pin.
Definition kshadernode.h:163
float defVec[4]
Default vector value when unconnected.
Definition kshadernode.h:168
float defFloat
Default scalar value when unconnected.
Definition kshadernode.h:167
float uiY
Cached screen-space position (set during draw).
Definition kshadernode.h:171
float uiX
Definition kshadernode.h:171
kString name
Display name of the pin.
Definition kshadernode.h:162
bool isOutput
True if this is an output pin.
Definition kshadernode.h:164