@@ -6,6 +6,8 @@ using Direct3D;
6
6
using Common ;
7
7
using System ;
8
8
using Win32 ;
9
+ using Common . Misc ;
10
+ using System . Threading ;
9
11
10
12
namespace Nanoforge . Render
11
13
{
@@ -22,6 +24,10 @@ namespace Nanoforge.Render
22
24
public ID3D11ShaderResourceView * View => _viewTexture . ShaderResourceView ;
23
25
public bool ErrorOccurred { get ; private set ; } = false ;
24
26
27
+ public append List < RenderObject > RenderObjects ~ ClearAndDeleteItems! ( _) ;
28
+ public append Dictionary < Material , List < RenderObject > > ObjectsByMaterial ~ ClearDictionaryAndDeleteValues! ( _) ;
29
+ private append Monitor _renderObjectCreationLock;
30
+
25
31
private ID3D11Device * _device = null ;
26
32
private ID3D11DeviceContext * _context = null ;
27
33
private ID3D11RasterizerState * _meshRasterizerState ~ ReleaseCOM ( & _) ;
@@ -50,6 +56,7 @@ namespace Nanoforge.Render
50
56
private bool _primitiveBufferNeedsUpdate = true ; //Set to true when the cpu side buffers have changed and need to be sent to the GPU
51
57
public bool PrimitiveMaterialsSet => _lineListMaterial != null && _triangleListMaterial != null && _litTriangleListMaterial != null ;
52
58
59
+ [ CRepr , RequiredSize ( 16 ) ]
53
60
private struct ColoredVertex
54
61
{
55
62
public Vec3 < f32 > Position ;
@@ -65,6 +72,7 @@ namespace Nanoforge.Render
65
72
}
66
73
}
67
74
75
+ [ CRepr , RequiredSize ( 28 ) ]
68
76
private struct ColoredVertexLit
69
77
{
70
78
public Vec3 < f32 > Position ;
@@ -85,17 +93,11 @@ namespace Nanoforge.Render
85
93
[ OnCompile ( . TypeInit ) ]
86
94
private static void ComptimeSizeChecks ( )
87
95
{
88
- //Make sure vertex structs are the expected size
89
- Runtime . Assert ( sizeof ( ColoredVertex ) == 16 ) ;
90
- Runtime . Assert ( sizeof ( ColoredVertexLit ) == 28 ) ;
91
-
92
96
//D3D11 constant buffers must align to 16 bytes
93
97
Runtime . Assert ( strideof ( PerFrameConstants ) % 16 == 0 ) ;
94
98
Runtime . Assert ( strideof ( PerObjectConstants ) % 16 == 0 ) ;
95
99
}
96
100
97
- //TODO: Add drawing functions
98
-
99
101
public this ( ID3D11Device * device , ID3D11DeviceContext * context )
100
102
{
101
103
_device = device ;
@@ -108,11 +110,6 @@ namespace Nanoforge.Render
108
110
Camera . Init ( . ( 312.615f , 56.846f , - 471.078f ) , 80.0f , . ( ViewWidth , ViewHeight ) , 1.0f , 10000.0f ) ;
109
111
}
110
112
111
- public ~ this ( )
112
- {
113
-
114
- }
115
-
116
113
public void Draw ( f32 deltaTime )
117
114
{
118
115
if ( ErrorOccurred || ! PrimitiveMaterialsSet )
@@ -137,9 +134,18 @@ namespace Nanoforge.Render
137
134
138
135
//Prepare state to render triangle strip meshes
139
136
_context . VSSetConstantBuffers ( 0 , 1 , & _perObjectConstantsBuffer . Ptr ) ;
140
- //_context.RSSetState(_meshRasterizerState);
141
- //_context.IASetPrimitiveTopology(.D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);
142
- //TODO: Add render objects tied to meshes and draw them here. Use to draw buildings, rocks, terrain, etc
137
+ _context . RSSetState ( _meshRasterizerState ) ;
138
+ _context . IASetPrimitiveTopology ( . D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP ) ;
139
+
140
+ //Render meshes
141
+ for ( var kv in ObjectsByMaterial)
142
+ {
143
+ //Batch render objects by material
144
+ Material material = kv. key;
145
+ material . Use ( _context ) ;
146
+ for ( RenderObject renderObject in kv . value )
147
+ renderObject . Draw ( _context , _perObjectConstantsBuffer , Camera ) ;
148
+ }
143
149
144
150
//Prepare state to render primitives
145
151
_context . RSSetState ( _primitiveRasterizerState ) ;
@@ -192,6 +198,25 @@ namespace Nanoforge.Render
192
198
ClearPrimitiveVertexBuffers ( ) ;
193
199
}
194
200
201
+ public Result < RenderObject > CreateRenderObject ( StringView materialName , Mesh mesh , Vec3 < f32 > position , Mat3 rotation )
202
+ {
203
+ ScopedLock ! ( _renderObjectCreationLock ) ;
204
+ if ( RenderMaterials . GetMaterial ( materialName ) case . Ok ( Material material ) )
205
+ {
206
+ RenderObject renderObject = new . ( mesh , position , rotation ) ;
207
+ RenderObjects . Add ( renderObject ) ;
208
+ if ( ! ObjectsByMaterial . ContainsKey ( material ) )
209
+ ObjectsByMaterial [ material ] = new List < RenderObject > ( ) ;
210
+
211
+ ObjectsByMaterial [ material ] . Add ( renderObject ) ;
212
+ return renderObject ;
213
+ }
214
+ else
215
+ {
216
+ return . Err ;
217
+ }
218
+ }
219
+
195
220
private void UpdatePrimitiveBuffers ( )
196
221
{
197
222
//Update line list vertex buffer
@@ -511,12 +536,4 @@ namespace Nanoforge.Render
511
536
public f32 Time = 0.0f ;
512
537
public Vec2 < f32 > ViewportDimensions = . Zero ;
513
538
}
514
-
515
- [ Align ( 16 ) , CRepr ]
516
- public struct PerObjectConstants
517
- {
518
- public Mat4 MVP ;
519
- public Mat4 Rotation ;
520
- public Vec4 < f32 > WorldPosition ;
521
- }
522
539
}
0 commit comments