Skip to content

Commit ac07022

Browse files
Refactor type registration
1 parent d4eca3d commit ac07022

File tree

15 files changed

+327
-147
lines changed

15 files changed

+327
-147
lines changed

building-a-renderer.md

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,26 @@ This data is sent to you through a series of `DrawCmd` instances.
1212

1313
```d
1414
struct DrawCmd {
15-
Texture[8] sources;
16-
uint32_t state;
17-
float opacity;
18-
uint32_t blendMode;
19-
uint32_t maskMode;
20-
uint32_t vtxOffset;
21-
uint32_t idxOffset;
22-
uint32_t elemCount;
15+
in_texture_t*[8] sources;
16+
in_drawstate_t state;
17+
in_blend_mode_t blendMode;
18+
in_mask_mode_t maskMode;
19+
uint32_t vtxOffset;
20+
uint32_t idxOffset;
21+
uint type;
22+
void[64] vars;
2323
}
2424
```
2525

2626
Inochi2D is capable of providing these draw lists with the assumption that your renderer supports index/element data, and optionally, vertex offsets. If your API does not support vertex offsets, set `useBaseVertex` in the Draw List to `false`.
2727

28+
### Variables
29+
30+
Some nodes in Inochi2D provide further data needed to render the the node, these are provided in
31+
`vars`, up to 64 bytes of variable space is allocated per draw command. Read the individual Node's
32+
documentation for which variables are stored within. The `type` variable can be used to determine which
33+
node type the data pertains to.
34+
2835
## DrawState
2936

3037
Inochi2D's rendering pipeline is rather complex, the framework supports masking and multi-layered compositing. As such, `DrawState` supplies flags for you to determine how to handle individual commands.
@@ -44,4 +51,30 @@ Inochi2D's rendering pipeline is rather complex, the framework supports masking
4451
You can then during `maskedDraw` add this texture to your pass, multiply `rgba` with `rrrr` from the mask texture.
4552
* On the transition from `maskedDraw` to `normal`, if you are not using unique shaders for `normal` you may want to clear the mask texture with all `0xFF`.
4653
* `compositeEnd` **may** be followed by `defineMask`, ensure that `compsiteBlit` is able to use the defined mask.
47-
* `compositeBlit` *should* be implemented by drawing a viewport-filling quad, an NDC mesh is provided for you in this DrawState to make it easier.
54+
* `compositeBlit` *should* be implemented by drawing a viewport-filling quad, an NDC mesh is provided for you in this DrawState to make it easier.
55+
56+
## Registered Types
57+
58+
Following is a list of all currently registered types, all types have a 32 bit type ID,
59+
with `0x00000000` to `0x0000FFFF` being reserved by Inochi2D.
60+
61+
Following is a table of all the base node types of Inochi2D.
62+
63+
| ID | Name |
64+
| ---------: | :-------------- |
65+
| 0x00000000 | Node |
66+
| 0x00000001 | Drawable |
67+
| 0x00000101 | Part |
68+
| 0x00000201 | AnimatedPart |
69+
| 0x00000002 | Deformer |
70+
| 0x00000102 | MeshGroup |
71+
| 0x00000202 | LatticeDeformer |
72+
| 0x00000003 | Driver |
73+
| 0x00000103 | SimplePhysics |
74+
| 0x00000004 | Composite |
75+
76+
All Inochi2D node types follow a numeric ID sequence of `0x0000SSBB` where
77+
* `SS` is the subnode id
78+
* `BB` is the supernode id
79+
80+
Part has ID 0x00000101, as it's type `01` (Part), derived from type `01` (Drawable)

source/inochi2d/cffi/render.d

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -217,14 +217,15 @@ enum in_blend_mode_t
217217
A drawing command from the Inochi2D draw list
218218
*/
219219
struct in_drawcmd_t {
220-
in_texture_t*[IN_MAX_ATTACHMENTS] sources;
221-
in_drawstate_t state;
222-
float opacity;
223-
in_blend_mode_t blendMode;
224-
in_mask_mode_t maskMode;
225-
uint vtxOffset;
226-
uint idxOffset;
227-
uint elemCount;
220+
in_texture_t*[IN_MAX_ATTACHMENTS] sources;
221+
in_drawstate_t state;
222+
in_blend_mode_t blendMode;
223+
in_mask_mode_t maskMode;
224+
uint vtxOffset;
225+
uint idxOffset;
226+
uint elemCount;
227+
uint type;
228+
void[64] vars;
228229
}
229230

230231
/**

source/inochi2d/core/nodes/composite/package.d

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,17 @@ import std.exception;
1818
public import inochi2d.core.render.state;
1919
import numem;
2020

21+
struct CompositeVars {
22+
align(vec4.sizeof):
23+
vec3 tint;
24+
vec3 screenTint;
25+
float opacity;
26+
}
27+
2128
/**
2229
Composite Node
2330
*/
24-
@TypeId("Composite")
31+
@TypeId("Composite", 0x0004)
2532
class Composite : Node {
2633
private:
2734
DrawListAlloc* __screenSpaceAlloc;
@@ -139,9 +146,6 @@ protected:
139146
vec3 offsetTint = vec3(0);
140147
vec3 offsetScreenTint = vec3(0);
141148

142-
override
143-
string typeId() { return "Composite"; }
144-
145149
// TODO: Cache this
146150
size_t maskCount() {
147151
size_t c;
@@ -332,6 +336,12 @@ public:
332336
if (!renderEnabled || toRender.length == 0)
333337
return;
334338

339+
CompositeVars compositeVars = CompositeVars(
340+
tint*offsetTint,
341+
screenTint*offsetScreenTint,
342+
opacity*offsetOpacity
343+
);
344+
335345
this.selfSort();
336346

337347
// Push sub render area.
@@ -348,7 +358,7 @@ public:
348358
}
349359

350360
// Then blit it to the main framebuffer
351-
drawList.setOpacity(offsetOpacity);
361+
drawList.setVariables!CompositeVars(nid, compositeVars);
352362
drawList.setMesh(__screenSpaceAlloc);
353363
drawList.setDrawState(DrawState.compositeBlit);
354364
drawList.setBlending(blendingMode);

source/inochi2d/core/nodes/deformer/latticedeformer.d

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import inmath.linalg;
1515
A deformer which uses a 2D lattice as the basis for
1616
its deformation.
1717
*/
18-
@TypeId("LatticeDeformer")
18+
@TypeId("LatticeDeformer", 0x0202)
1919
class LatticeDeformer : Deformer {
2020
private:
2121
int subdivs;
@@ -73,6 +73,4 @@ public:
7373
override void resetDeform() {
7474

7575
}
76-
77-
override string typeId() { return "LatticeDeformer"; }
7876
}

source/inochi2d/core/nodes/deformer/meshdeformer.d

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import numem;
2020
A deformer which deforms child nodes stored within it,
2121
2222
*/
23-
@TypeId("MeshGroup")
23+
@TypeId("MeshGroup", 0x0102)
2424
class MeshDeformer : Deformer {
2525
private:
2626
Mesh mesh_;
@@ -247,9 +247,6 @@ public:
247247
super.rescan();
248248
this.rebuildStructures();
249249
}
250-
251-
override
252-
string typeId() { return "MeshGroup"; }
253250
}
254251

255252
private

source/inochi2d/core/nodes/deformer/package.d

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ public import inochi2d.core.nodes.deformer.latticedeformer;
2121
2222
Deformations happen in world space
2323
*/
24+
@TypeId("Deformer", 0x0002)
25+
@TypeIdAbstract
2426
abstract
2527
class Deformer : Node, IDeformable {
2628
private:

source/inochi2d/core/nodes/drawable/apart.d

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,7 @@ import inochi2d.core.math;
66
/**
77
Parts which contain spritesheet animation
88
*/
9-
@TypeId("AnimatedPart")
9+
@TypeId("AnimatedPart", 0x0201)
1010
class AnimatedPart : Part {
11-
private:
1211

13-
protected:
14-
override
15-
string typeId() { return "AnimatedPart"; }
16-
17-
public:
18-
19-
/**
20-
The amount of splits in the texture
21-
*/
22-
vec2i splits;
2312
}

source/inochi2d/core/nodes/drawable/package.d

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,15 @@ public import inochi2d.core.mesh;
2020
public import inochi2d.core.nodes.drawable.part;
2121
public import inochi2d.core.nodes.drawable.apart;
2222

23-
package(inochi2d)
24-
void inInitDrawable() {
25-
inRegisterNodeType!Part;
26-
}
27-
2823
/**
2924
Nodes that are meant to render something in to the Inochi2D scene
3025
Other nodes don't have to render anything and serve mostly other
3126
purposes.
3227
3328
The main types of Drawables are Parts and Masks
3429
*/
35-
36-
@TypeId("Drawable")
30+
@TypeId("Drawable", 0x0001)
31+
@TypeIdAbstract
3732
abstract class Drawable : Node, IDeformable {
3833
private:
3934
Mesh mesh_;
@@ -192,7 +187,4 @@ public:
192187
void drawAsMask(float delta, DrawList drawList, MaskingMode mode) {
193188
drawList.setMesh(drawListSlot);
194189
}
195-
196-
override
197-
string typeId() { return "Drawable"; }
198190
}

source/inochi2d/core/nodes/drawable/part.d

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,22 @@ enum TextureUsage : size_t {
2828
COUNT
2929
}
3030

31+
struct PartVars {
32+
align(vec4.sizeof):
33+
vec3 tint;
34+
vec3 screenTint;
35+
float opacity;
36+
float emissionStrength;
37+
}
38+
3139
/**
3240
Dynamic Mesh Part
3341
*/
34-
@TypeId("Part")
42+
@TypeId("Part", 0x0101)
3543
class Part : Drawable {
3644
private:
3745
protected:
3846

39-
override
40-
string typeId() { return "Part"; }
41-
4247
/**
4348
Allows serializing self data (with pretty serializer)
4449
*/
@@ -101,19 +106,6 @@ protected:
101106
vec3 offsetTint = vec3(0);
102107
vec3 offsetScreenTint = vec3(0);
103108

104-
// TODO: Cache this
105-
size_t maskCount() {
106-
size_t c;
107-
foreach(m; masks) if (m.mode == MaskingMode.mask) c++;
108-
return c;
109-
}
110-
111-
size_t dodgeCount() {
112-
size_t c;
113-
foreach(m; masks) if (m.mode == MaskingMode.dodge) c++;
114-
return c;
115-
}
116-
117109
public:
118110
/**
119111
List of textures this part can use
@@ -273,16 +265,15 @@ public:
273265
override
274266
float getValue(string key) {
275267
switch(key) {
276-
case "alphaThreshold": return offsetMaskThreshold;
277-
case "opacity": return offsetOpacity;
278-
case "tint.r": return offsetTint.x;
279-
case "tint.g": return offsetTint.y;
280-
case "tint.b": return offsetTint.z;
281-
case "screenTint.r": return offsetScreenTint.x;
282-
case "screenTint.g": return offsetScreenTint.y;
283-
case "screenTint.b": return offsetScreenTint.z;
268+
case "opacity": return offsetOpacity;
269+
case "tint.r": return offsetTint.x;
270+
case "tint.g": return offsetTint.y;
271+
case "tint.b": return offsetTint.z;
272+
case "screenTint.r": return offsetScreenTint.x;
273+
case "screenTint.g": return offsetScreenTint.y;
274+
case "screenTint.b": return offsetScreenTint.z;
284275
case "emissionStrength": return offsetEmissionStrength;
285-
default: return super.getValue(key);
276+
default: return super.getValue(key);
286277
}
287278
}
288279

@@ -322,6 +313,13 @@ public:
322313
void draw(float delta, DrawList drawList) {
323314
if (!renderEnabled)
324315
return;
316+
317+
PartVars vars = PartVars(
318+
tint*offsetTint,
319+
screenTint*offsetScreenTint,
320+
opacity*offsetOpacity,
321+
emissionStrength*offsetEmissionStrength
322+
);
325323

326324
if (masks.length > 0) {
327325
foreach(ref mask; masks) {
@@ -331,7 +329,7 @@ public:
331329

332330
super.draw(delta, drawList);
333331
drawList.setDrawState(DrawState.maskedDraw);
334-
drawList.setOpacity(offsetOpacity);
332+
drawList.setVariables!PartVars(nid, vars);
335333
drawList.setBlending(blendingMode);
336334
drawList.setSources(textures);
337335
drawList.next();
@@ -341,7 +339,7 @@ public:
341339
super.draw(delta, drawList);
342340
drawList.setSources(textures);
343341
drawList.setBlending(blendingMode);
344-
drawList.setOpacity(offsetOpacity);
342+
drawList.setVariables!PartVars(nid, vars);
345343
drawList.next();
346344
}
347345

source/inochi2d/core/nodes/drivers/package.d

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ public import inochi2d.core.nodes.drivers.simplephysics;
1515
/**
1616
Driver abstract node type
1717
*/
18-
@TypeId("Driver")
18+
@TypeId("Driver", 0x00000003)
19+
@TypeIdAbstract
1920
abstract class Driver : Node {
2021
protected:
2122
this() { }

0 commit comments

Comments
 (0)