From 698570c788117b082ceb7b357005e54d30b358ea Mon Sep 17 00:00:00 2001 From: Selinux24 Date: Sat, 27 Jul 2024 11:05:03 +0200 Subject: [PATCH] Builtin classes refactoring New component to draw dynamic geometry with custom materials --- .../Components/Cubemap}/CubeMapGeometry.cs | 2 +- .../Components/Cubemap}/Cubemap.cs | 3 +- .../Components/Cubemap}/CubemapDescription.cs | 2 +- .../Components/Cubemap}/CubemapState.cs | 2 +- .../BuiltIn/Components/Decals/DecalDrawer.cs | 1 + .../Components/Foliage/FoliageBuffer.cs | 1 + .../Components/Foliage/FoliagePatch.cs | 3 +- Engine/BuiltIn/Components/Ground/Scenery.cs | 1 + Engine/BuiltIn/Components/Ground/Terrain.cs | 1 + .../BuiltIn/Components/Ground/TerrainGrid.cs | 1 + Engine/BuiltIn/Components/Model/Model.cs | 2 +- .../Components/Model/ModelInstanced.cs | 7 +- .../Components/Particles/ParticleSystemCPU.cs | 1 + .../Components/Particles/ParticleSystemGPU.cs | 1 + .../Primitives/GeometryColorDrawer.cs | 1 + .../Components/Primitives/GeometryDrawer.cs | 283 ++++++++++++++++++ .../Primitives/GeometryDrawerDescription.cs | 58 ++++ Engine/BuiltIn/Components/Skies/SkyPlane.cs | 1 + .../BuiltIn/Components/Skies/SkyScattering.cs | 1 + Engine/BuiltIn/Components/Skies/Skydom.cs | 2 + .../Components/Skies/SkydomDescription.cs | 2 + Engine/BuiltIn/Components/Water/Water.cs | 1 + Engine/BuiltIn/Drawers/BuiltInDrawer.cs | 28 +- .../Drawers/Deferred/DeferredDrawerManager.cs | 2 +- .../Drawers/Forward/ForwardDrawerManager.cs | 2 +- .../Primitives}/VertexBillboard.cs | 4 +- .../Primitives}/VertexCPUParticle.cs | 4 +- .../Primitives}/VertexDecal.cs | 4 +- .../Primitives}/VertexFont.cs | 4 +- .../Primitives}/VertexGPUParticle.cs | 4 +- .../Primitives}/VertexInstancingData.cs | 3 +- .../Primitives}/VertexPosition.cs | 6 +- .../Primitives}/VertexPositionColor.cs | 4 +- .../Primitives}/VertexPositionNormalColor.cs | 4 +- .../VertexPositionNormalTexture.cs | 4 +- .../VertexPositionNormalTextureTangent.cs | 4 +- .../Primitives}/VertexPositionTexture.cs | 4 +- .../Primitives}/VertexSkinnedPosition.cs | 6 +- .../Primitives}/VertexSkinnedPositionColor.cs | 6 +- .../VertexSkinnedPositionNormalColor.cs | 6 +- .../VertexSkinnedPositionNormalTexture.cs | 6 +- ...rtexSkinnedPositionNormalTextureTangent.cs | 6 +- .../VertexSkinnedPositionTexture.cs | 6 +- .../Primitives}/VertexTerrain.cs | 4 +- .../Primitives}/VertexTypes.cs | 3 +- Engine/Common/BaseModel.cs | 24 -- .../BufferDescriptorRequestInstancing.cs | 14 +- .../Common/BufferDescriptorRequestVertices.cs | 1 + Engine/Common/BufferManager.cs | 50 ++-- Engine/Common/BufferManagerInstances.cs | 18 +- Engine/Common/BufferManagerVertices.cs | 1 + Engine/Common/IEngineBufferDescriptor.cs | 40 +-- Engine/Common/IEngineDescriptor.cs | 48 +++ .../IEngineInstancingBufferDescriptor.cs | 29 ++ Engine/Common/Mesh.cs | 3 +- Engine/Common/VertexData.cs | 3 +- Engine/Content/ContentData.cs | 1 + Engine/Content/SubMeshContent.cs | 1 + Engine/GameDrawingHelper.cs | 4 +- Engine/Graphics.Buffers.cs | 1 + Engine/IShadowMap.cs | 1 + Engine/IVertexData.cs | 1 + Engine/PostProcessingDrawer.cs | 1 + Engine/SceneRendererDeferredLights.cs | 1 + Engine/ShadowMap.cs | 1 + Engine/UI/FontMapSentenceDescriptor.cs | 2 +- Engine/UI/FontMapWordDescriptor.cs | 2 +- Engine/UI/Sprite.cs | 1 + Engine/UI/UITextureRenderer.cs | 1 + 69 files changed, 592 insertions(+), 158 deletions(-) rename Engine/{ => BuiltIn/Components/Cubemap}/CubeMapGeometry.cs (89%) rename Engine/{ => BuiltIn/Components/Cubemap}/Cubemap.cs (98%) rename Engine/{ => BuiltIn/Components/Cubemap}/CubemapDescription.cs (99%) rename Engine/{ => BuiltIn/Components/Cubemap}/CubemapState.cs (86%) create mode 100644 Engine/BuiltIn/Components/Primitives/GeometryDrawer.cs create mode 100644 Engine/BuiltIn/Components/Primitives/GeometryDrawerDescription.cs rename Engine/{Common => BuiltIn/Primitives}/VertexBillboard.cs (97%) rename Engine/{Common => BuiltIn/Primitives}/VertexCPUParticle.cs (97%) rename Engine/{Common => BuiltIn/Primitives}/VertexDecal.cs (98%) rename Engine/{Common => BuiltIn/Primitives}/VertexFont.cs (98%) rename Engine/{Common => BuiltIn/Primitives}/VertexGPUParticle.cs (98%) rename Engine/{Common => BuiltIn/Primitives}/VertexInstancingData.cs (98%) rename Engine/{Common => BuiltIn/Primitives}/VertexPosition.cs (97%) rename Engine/{Common => BuiltIn/Primitives}/VertexPositionColor.cs (98%) rename Engine/{Common => BuiltIn/Primitives}/VertexPositionNormalColor.cs (98%) rename Engine/{Common => BuiltIn/Primitives}/VertexPositionNormalTexture.cs (98%) rename Engine/{Common => BuiltIn/Primitives}/VertexPositionNormalTextureTangent.cs (98%) rename Engine/{Common => BuiltIn/Primitives}/VertexPositionTexture.cs (98%) rename Engine/{Common => BuiltIn/Primitives}/VertexSkinnedPosition.cs (97%) rename Engine/{Common => BuiltIn/Primitives}/VertexSkinnedPositionColor.cs (97%) rename Engine/{Common => BuiltIn/Primitives}/VertexSkinnedPositionNormalColor.cs (97%) rename Engine/{Common => BuiltIn/Primitives}/VertexSkinnedPositionNormalTexture.cs (98%) rename Engine/{Common => BuiltIn/Primitives}/VertexSkinnedPositionNormalTextureTangent.cs (98%) rename Engine/{Common => BuiltIn/Primitives}/VertexSkinnedPositionTexture.cs (97%) rename Engine/{Common => BuiltIn/Primitives}/VertexTerrain.cs (98%) rename Engine/{Common => BuiltIn/Primitives}/VertexTypes.cs (98%) create mode 100644 Engine/Common/IEngineDescriptor.cs create mode 100644 Engine/Common/IEngineInstancingBufferDescriptor.cs diff --git a/Engine/CubeMapGeometry.cs b/Engine/BuiltIn/Components/Cubemap/CubeMapGeometry.cs similarity index 89% rename from Engine/CubeMapGeometry.cs rename to Engine/BuiltIn/Components/Cubemap/CubeMapGeometry.cs index 9644c8f5f..3353a82e9 100644 --- a/Engine/CubeMapGeometry.cs +++ b/Engine/BuiltIn/Components/Cubemap/CubeMapGeometry.cs @@ -1,5 +1,5 @@  -namespace Engine +namespace Engine.BuiltIn.Components.Cubemap { /// /// Cube map geometry enumeration diff --git a/Engine/Cubemap.cs b/Engine/BuiltIn/Components/Cubemap/Cubemap.cs similarity index 98% rename from Engine/Cubemap.cs rename to Engine/BuiltIn/Components/Cubemap/Cubemap.cs index 867f853ca..76acf5b7e 100644 --- a/Engine/Cubemap.cs +++ b/Engine/BuiltIn/Components/Cubemap/Cubemap.cs @@ -3,10 +3,11 @@ using System.Collections.Generic; using System.Threading.Tasks; -namespace Engine +namespace Engine.BuiltIn.Components.Cubemap { using Engine.BuiltIn.Drawers; using Engine.BuiltIn.Drawers.Cubemap; + using Engine.BuiltIn.Primitives; using Engine.Common; using Engine.Content; diff --git a/Engine/CubemapDescription.cs b/Engine/BuiltIn/Components/Cubemap/CubemapDescription.cs similarity index 99% rename from Engine/CubemapDescription.cs rename to Engine/BuiltIn/Components/Cubemap/CubemapDescription.cs index 01bb638c0..95657cb29 100644 --- a/Engine/CubemapDescription.cs +++ b/Engine/BuiltIn/Components/Cubemap/CubemapDescription.cs @@ -1,6 +1,6 @@ using SharpDX; -namespace Engine +namespace Engine.BuiltIn.Components.Cubemap { /// /// Cube-map description diff --git a/Engine/CubemapState.cs b/Engine/BuiltIn/Components/Cubemap/CubemapState.cs similarity index 86% rename from Engine/CubemapState.cs rename to Engine/BuiltIn/Components/Cubemap/CubemapState.cs index 4fdce5cfb..c9d0b2b7f 100644 --- a/Engine/CubemapState.cs +++ b/Engine/BuiltIn/Components/Cubemap/CubemapState.cs @@ -1,5 +1,5 @@  -namespace Engine +namespace Engine.BuiltIn.Components.Cubemap { using Engine.Common; diff --git a/Engine/BuiltIn/Components/Decals/DecalDrawer.cs b/Engine/BuiltIn/Components/Decals/DecalDrawer.cs index 3a90466df..9636d2e42 100644 --- a/Engine/BuiltIn/Components/Decals/DecalDrawer.cs +++ b/Engine/BuiltIn/Components/Decals/DecalDrawer.cs @@ -6,6 +6,7 @@ namespace Engine.BuiltIn.Components.Decals { using Engine.BuiltIn.Drawers; using Engine.BuiltIn.Drawers.Decals; + using Engine.BuiltIn.Primitives; using Engine.Common; using Engine.Content; diff --git a/Engine/BuiltIn/Components/Foliage/FoliageBuffer.cs b/Engine/BuiltIn/Components/Foliage/FoliageBuffer.cs index 5941decea..00f620f22 100644 --- a/Engine/BuiltIn/Components/Foliage/FoliageBuffer.cs +++ b/Engine/BuiltIn/Components/Foliage/FoliageBuffer.cs @@ -1,4 +1,5 @@ using Engine.BuiltIn.Drawers; +using Engine.BuiltIn.Primitives; using Engine.Common; using System; diff --git a/Engine/BuiltIn/Components/Foliage/FoliagePatch.cs b/Engine/BuiltIn/Components/Foliage/FoliagePatch.cs index 514330fae..616de0561 100644 --- a/Engine/BuiltIn/Components/Foliage/FoliagePatch.cs +++ b/Engine/BuiltIn/Components/Foliage/FoliagePatch.cs @@ -1,4 +1,5 @@ -using Engine.Collections; +using Engine.BuiltIn.Primitives; +using Engine.Collections; using Engine.Common; using SharpDX; using System; diff --git a/Engine/BuiltIn/Components/Ground/Scenery.cs b/Engine/BuiltIn/Components/Ground/Scenery.cs index bcc919877..ad07f00cb 100644 --- a/Engine/BuiltIn/Components/Ground/Scenery.cs +++ b/Engine/BuiltIn/Components/Ground/Scenery.cs @@ -11,6 +11,7 @@ namespace Engine.BuiltIn.Components.Ground using Engine.BuiltIn.Drawers; using Engine.BuiltIn.Drawers.Deferred; using Engine.BuiltIn.Drawers.Forward; + using Engine.BuiltIn.Primitives; using Engine.Collections.Generic; using Engine.Common; using Engine.Content; diff --git a/Engine/BuiltIn/Components/Ground/Terrain.cs b/Engine/BuiltIn/Components/Ground/Terrain.cs index b5f00e99b..ca525baa8 100644 --- a/Engine/BuiltIn/Components/Ground/Terrain.cs +++ b/Engine/BuiltIn/Components/Ground/Terrain.cs @@ -9,6 +9,7 @@ namespace Engine.BuiltIn.Components.Ground using Engine.BuiltIn.Drawers; using Engine.BuiltIn.Drawers.Common; using Engine.BuiltIn.Drawers.Deferred; + using Engine.BuiltIn.Primitives; using Engine.Common; using Engine.Content; diff --git a/Engine/BuiltIn/Components/Ground/TerrainGrid.cs b/Engine/BuiltIn/Components/Ground/TerrainGrid.cs index 842e674a4..7ae9361e0 100644 --- a/Engine/BuiltIn/Components/Ground/TerrainGrid.cs +++ b/Engine/BuiltIn/Components/Ground/TerrainGrid.cs @@ -7,6 +7,7 @@ namespace Engine.BuiltIn.Components.Ground { using Engine.BuiltIn.Drawers; + using Engine.BuiltIn.Primitives; using Engine.Collections.Generic; using Engine.Common; diff --git a/Engine/BuiltIn/Components/Model/Model.cs b/Engine/BuiltIn/Components/Model/Model.cs index 1f3d9250d..08fbdaebe 100644 --- a/Engine/BuiltIn/Components/Model/Model.cs +++ b/Engine/BuiltIn/Components/Model/Model.cs @@ -281,7 +281,7 @@ public override bool Draw(DrawContext context) continue; } - var drawer = GetDrawer(context.DrawerMode, mesh.VertextType, false); + var drawer = BuiltInDrawer.GetDrawer(context.DrawerMode, mesh.VertextType, false); if (drawer == null) { continue; diff --git a/Engine/BuiltIn/Components/Model/ModelInstanced.cs b/Engine/BuiltIn/Components/Model/ModelInstanced.cs index 014cd4cc9..07684c2da 100644 --- a/Engine/BuiltIn/Components/Model/ModelInstanced.cs +++ b/Engine/BuiltIn/Components/Model/ModelInstanced.cs @@ -9,6 +9,7 @@ namespace Engine.BuiltIn.Components.Models { using Engine.BuiltIn.Drawers; + using Engine.BuiltIn.Primitives; using Engine.Common; /// @@ -152,7 +153,7 @@ protected override void Dispose(bool disposing) { if (disposing) { - BufferManager.RemoveInstancingData(InstancingBuffer); + BufferManager.RemoveInstancingData(InstancingBuffer); InstancingBuffer = null; } @@ -182,7 +183,7 @@ public override async Task ReadAssets(ModelInstancedDescription description) /// public override async Task InitializeAssets() { - InstancingBuffer = BufferManager.AddInstancingData($"{Name}.Instances", true, Description.Instances); + InstancingBuffer = BufferManager.AddInstancingData($"{Name}.Instances", true, Description.Instances); foreach (var drawable in GetDrawingDataCollection()) { @@ -493,7 +494,7 @@ private int DrawMesh(DrawContext context, DrawingData drawingData, ModelInstance continue; } - var drawer = GetDrawer(context.DrawerMode, mesh.VertextType, true); + var drawer = BuiltInDrawer.GetDrawer(context.DrawerMode, mesh.VertextType, true); if (drawer == null) { continue; diff --git a/Engine/BuiltIn/Components/Particles/ParticleSystemCPU.cs b/Engine/BuiltIn/Components/Particles/ParticleSystemCPU.cs index 82567543a..a014d5779 100644 --- a/Engine/BuiltIn/Components/Particles/ParticleSystemCPU.cs +++ b/Engine/BuiltIn/Components/Particles/ParticleSystemCPU.cs @@ -5,6 +5,7 @@ namespace Engine.BuiltIn.Components.Particles { using Engine.BuiltIn.Drawers; using Engine.BuiltIn.Drawers.Particles; + using Engine.BuiltIn.Primitives; using Engine.Common; using Engine.Content; diff --git a/Engine/BuiltIn/Components/Particles/ParticleSystemGPU.cs b/Engine/BuiltIn/Components/Particles/ParticleSystemGPU.cs index 51c678296..785734f88 100644 --- a/Engine/BuiltIn/Components/Particles/ParticleSystemGPU.cs +++ b/Engine/BuiltIn/Components/Particles/ParticleSystemGPU.cs @@ -5,6 +5,7 @@ namespace Engine.BuiltIn.Components.Particles { using Engine.BuiltIn.Drawers; using Engine.BuiltIn.Drawers.Particles; + using Engine.BuiltIn.Primitives; using Engine.Common; using Engine.Content; diff --git a/Engine/BuiltIn/Components/Primitives/GeometryColorDrawer.cs b/Engine/BuiltIn/Components/Primitives/GeometryColorDrawer.cs index cf95b973a..063b28ab5 100644 --- a/Engine/BuiltIn/Components/Primitives/GeometryColorDrawer.cs +++ b/Engine/BuiltIn/Components/Primitives/GeometryColorDrawer.cs @@ -8,6 +8,7 @@ namespace Engine.BuiltIn.Components.Primitives { using Engine.BuiltIn.Drawers; using Engine.BuiltIn.Drawers.Forward; + using Engine.BuiltIn.Primitives; using Engine.Common; /// diff --git a/Engine/BuiltIn/Components/Primitives/GeometryDrawer.cs b/Engine/BuiltIn/Components/Primitives/GeometryDrawer.cs new file mode 100644 index 000000000..9eeb55b44 --- /dev/null +++ b/Engine/BuiltIn/Components/Primitives/GeometryDrawer.cs @@ -0,0 +1,283 @@ +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace Engine.BuiltIn.Components.Primitives +{ + using Engine.BuiltIn.Drawers; + using Engine.BuiltIn.Primitives; + using Engine.Common; + + /// + /// Geometry drawer + /// + /// Geometry type + /// + /// Constructor + /// + /// Scene + /// Id + /// Name + public sealed class GeometryDrawer(Scene scene, string id, string name) : Drawable>(scene, id, name), ITransformable3D where T : struct, IVertexData + { + const string className = nameof(GeometryDrawer); + + /// + /// Triangle dictionary by color + /// + private readonly ConcurrentBag bag = []; + /// + /// Vertex type + /// + private readonly VertexTypes vertexType = default(T).VertexType; + + /// + /// Topology + /// + private Topology topology; + /// + /// Material + /// + private IMeshMaterial material; + /// + /// Bag changed flag + /// + private bool bagChanged = false; + /// + /// Vertex buffer descriptor + /// + private BufferDescriptor vertexBuffer = null; + /// + /// Vertices to draw + /// + private int vertexCount = 0; + + /// + /// Returns true if the buffers were ready + /// + public bool BuffersReady + { + get + { + if (vertexBuffer?.Ready != true) + { + return false; + } + + return true; + } + } + /// + public IManipulator3D Manipulator { get; private set; } = new Manipulator3D(); + + /// + /// Destructor + /// + ~GeometryDrawer() + { + // Finalizer calls Dispose(false) + Dispose(false); + } + /// + protected override void Dispose(bool disposing) + { + if (disposing) + { + //Remove data from buffer manager + BufferManager?.RemoveVertexData(vertexBuffer); + } + } + + /// + public override async Task ReadAssets(GeometryDrawerDescription description) + { + await base.ReadAssets(description); + + if (description.Topology == Topology.Undefined) + { + throw new EngineException("Topology is not defined."); + } + + topology = description.Topology; + material = description.ReadMaterial(); + + var primitives = description.Vertices ?? []; + + int count; + if (primitives.Length > 0) + { + count = Description.Vertices.Length; + + foreach (var item in primitives) + { + bag.Add(item); + } + bagChanged = true; + } + else + { + count = Description.Count; + + bagChanged = false; + } + + InitializeBuffers(Name, count); + } + + /// + /// Initialize buffers + /// + /// Name + /// Vertex count + private void InitializeBuffers(string name, int vertexCount) + { + vertexBuffer = BufferManager.AddVertexData(name, true, new T[vertexCount]); + } + /// + /// Set primitive + /// + /// Primitive + public void SetPrimitives(T primitive) + { + SetPrimitives([primitive]); + } + /// + /// Set primitives list + /// + /// Primitives list + public void SetPrimitives(IEnumerable primitives) + { + bag.Clear(); + + if (!(primitives?.Any() ?? false)) + { + return; + } + + foreach (var item in primitives) + { + bag.Add(item); + } + bagChanged = true; + } + /// + /// Add primitive to list + /// + /// primitive + public void AddPrimitives(T primitive) + { + AddPrimitives([primitive]); + } + /// + /// Add primitives to list + /// + /// Primitives list + public void AddPrimitives(IEnumerable primitives) + { + if (!(primitives?.Any() ?? false)) + { + return; + } + + foreach (var item in primitives) + { + bag.Add(item); + } + bagChanged = true; + } + /// + /// Remove all + /// + public void Clear() + { + bag.Clear(); + bagChanged = true; + } + + /// + public void SetManipulator(IManipulator3D manipulator) + { + if (manipulator == null) + { + Logger.WriteWarning(this, $"{className} Name: {Name} - Sets a null manipulator. Discarded."); + + return; + } + + Manipulator = manipulator; + } + + /// + public override bool Draw(DrawContext context) + { + if (!Visible) + { + return false; + } + + if (!BuffersReady) + { + return false; + } + + bool draw = context.ValidateDraw(BlendMode); + if (!draw) + { + return false; + } + + var dc = context.DeviceContext; + + WriteDataInBuffer(dc); + + if (vertexCount <= 0) + { + return false; + } + + var drawer = BuiltInDrawer.GetDrawer(context.DrawerMode, vertexType, false); + if (drawer == null) + { + return false; + } + + drawer.UpdateMesh(dc, BuiltInDrawerMeshState.SetLocal(Manipulator.GlobalTransform)); + drawer.UpdateMaterial(dc, new BuiltInDrawerMaterialState() + { + Material = material, + }); + + bool drawn = drawer.Draw(dc, new DrawOptions + { + VertexBuffer = vertexBuffer, + VertexDrawCount = vertexCount, + Topology = topology, + }); + + return drawn; + } + /// + /// Writes dictionary data in buffer + /// + /// Device context + public void WriteDataInBuffer(IEngineDeviceContext dc) + { + if (!bagChanged) + { + return; + } + + var copy = bag.ToArray(); + + if (!Game.WriteVertexBuffer(dc, vertexBuffer, copy)) + { + return; + } + + vertexCount = copy.Length; + + bagChanged = false; + } + } +} diff --git a/Engine/BuiltIn/Components/Primitives/GeometryDrawerDescription.cs b/Engine/BuiltIn/Components/Primitives/GeometryDrawerDescription.cs new file mode 100644 index 000000000..b9c061faa --- /dev/null +++ b/Engine/BuiltIn/Components/Primitives/GeometryDrawerDescription.cs @@ -0,0 +1,58 @@ +using Engine.Common; +using Engine.Content; + +namespace Engine.BuiltIn.Components.Primitives +{ + /// + /// Geometry drawer description + /// + public class GeometryDrawerDescription : BaseModelDescription where T : IVertexData + { + /// + /// Maximum triangle count + /// + public int Count { get; set; } = 1000; + /// + /// Initial vertices + /// + public T[] Vertices { get; set; } + /// + /// Topology + /// + public Topology Topology { get; set; } + /// + /// Mesh image data collection + /// + public (string Name, IImageContent Content)[] Textures { get; set; } = []; + /// + /// Material content + /// + public IMaterialContent Material { get; set; } = MaterialBlinnPhongContent.Default; + + /// + /// Constructor + /// + public GeometryDrawerDescription() + : base() + { + DeferredEnabled = true; + DepthEnabled = true; + } + + /// + /// Reads the material data + /// + public IMeshMaterial ReadMaterial() + { + var textures = Textures ?? []; + + MeshImageDataCollection values = new(); + foreach (var texture in textures) + { + values.SetValue(texture.Name, MeshImageData.FromContent(texture.Content)); + } + + return Material?.CreateMeshMaterial(values); + } + } +} diff --git a/Engine/BuiltIn/Components/Skies/SkyPlane.cs b/Engine/BuiltIn/Components/Skies/SkyPlane.cs index 153219b50..2fbf84157 100644 --- a/Engine/BuiltIn/Components/Skies/SkyPlane.cs +++ b/Engine/BuiltIn/Components/Skies/SkyPlane.cs @@ -6,6 +6,7 @@ namespace Engine.BuiltIn.Components.Skies { using Engine.BuiltIn.Drawers; using Engine.BuiltIn.Drawers.Clouds; + using Engine.BuiltIn.Primitives; using Engine.Common; using Engine.Content; diff --git a/Engine/BuiltIn/Components/Skies/SkyScattering.cs b/Engine/BuiltIn/Components/Skies/SkyScattering.cs index ceb4dc8a4..057ecf117 100644 --- a/Engine/BuiltIn/Components/Skies/SkyScattering.cs +++ b/Engine/BuiltIn/Components/Skies/SkyScattering.cs @@ -6,6 +6,7 @@ namespace Engine.BuiltIn.Components.Skies { using Engine.BuiltIn.Drawers; using Engine.BuiltIn.Drawers.SkyScattering; + using Engine.BuiltIn.Primitives; using Engine.Common; /// diff --git a/Engine/BuiltIn/Components/Skies/Skydom.cs b/Engine/BuiltIn/Components/Skies/Skydom.cs index 342bb5765..929fb15cc 100644 --- a/Engine/BuiltIn/Components/Skies/Skydom.cs +++ b/Engine/BuiltIn/Components/Skies/Skydom.cs @@ -1,4 +1,6 @@  +using Engine.BuiltIn.Components.Cubemap; + namespace Engine.BuiltIn.Components.Skies { /// diff --git a/Engine/BuiltIn/Components/Skies/SkydomDescription.cs b/Engine/BuiltIn/Components/Skies/SkydomDescription.cs index bc88232e8..d4030f996 100644 --- a/Engine/BuiltIn/Components/Skies/SkydomDescription.cs +++ b/Engine/BuiltIn/Components/Skies/SkydomDescription.cs @@ -1,4 +1,6 @@  +using Engine.BuiltIn.Components.Cubemap; + namespace Engine.BuiltIn.Components.Skies { /// diff --git a/Engine/BuiltIn/Components/Water/Water.cs b/Engine/BuiltIn/Components/Water/Water.cs index 057c04534..d38e9c9b1 100644 --- a/Engine/BuiltIn/Components/Water/Water.cs +++ b/Engine/BuiltIn/Components/Water/Water.cs @@ -4,6 +4,7 @@ namespace Engine.BuiltIn.Components.Water { using Engine.BuiltIn.Drawers; using Engine.BuiltIn.Drawers.Water; + using Engine.BuiltIn.Primitives; using Engine.Common; /// diff --git a/Engine/BuiltIn/Drawers/BuiltInDrawer.cs b/Engine/BuiltIn/Drawers/BuiltInDrawer.cs index 86890112f..f78fbb742 100644 --- a/Engine/BuiltIn/Drawers/BuiltInDrawer.cs +++ b/Engine/BuiltIn/Drawers/BuiltInDrawer.cs @@ -1,7 +1,10 @@ - +using Engine.BuiltIn.Drawers.Deferred; +using Engine.BuiltIn.Drawers.Forward; +using Engine.BuiltIn.Primitives; +using Engine.Common; + namespace Engine.BuiltIn.Drawers { - using Engine.Common; using Format = SharpDX.DXGI.Format; /// @@ -461,5 +464,26 @@ public void StreamOut(IEngineDeviceContext dc, bool firstRun, IEngineVertexBuffe dc.SetGeometryShaderStreamOutputTargets(null); } + + /// + /// Gets the drawing effect for the current instance + /// + /// Drawing mode + /// Vertex type + /// Returns the drawing effect + public static IBuiltInDrawer GetDrawer(DrawerModes mode, VertexTypes vertexType, bool instanced) + { + if (mode.HasFlag(DrawerModes.Forward)) + { + return ForwardDrawerManager.GetDrawer(vertexType, instanced); + } + + if (mode.HasFlag(DrawerModes.Deferred)) + { + return DeferredDrawerManager.GetDrawer(vertexType, instanced); + } + + return null; + } } } diff --git a/Engine/BuiltIn/Drawers/Deferred/DeferredDrawerManager.cs b/Engine/BuiltIn/Drawers/Deferred/DeferredDrawerManager.cs index a3cf286f8..71a1e3b4a 100644 --- a/Engine/BuiltIn/Drawers/Deferred/DeferredDrawerManager.cs +++ b/Engine/BuiltIn/Drawers/Deferred/DeferredDrawerManager.cs @@ -1,4 +1,4 @@ -using Engine.Common; +using Engine.BuiltIn.Primitives; namespace Engine.BuiltIn.Drawers.Deferred { diff --git a/Engine/BuiltIn/Drawers/Forward/ForwardDrawerManager.cs b/Engine/BuiltIn/Drawers/Forward/ForwardDrawerManager.cs index 9115b85e4..ecce82b93 100644 --- a/Engine/BuiltIn/Drawers/Forward/ForwardDrawerManager.cs +++ b/Engine/BuiltIn/Drawers/Forward/ForwardDrawerManager.cs @@ -1,4 +1,4 @@ -using Engine.Common; +using Engine.BuiltIn.Primitives; namespace Engine.BuiltIn.Drawers.Forward { diff --git a/Engine/Common/VertexBillboard.cs b/Engine/BuiltIn/Primitives/VertexBillboard.cs similarity index 97% rename from Engine/Common/VertexBillboard.cs rename to Engine/BuiltIn/Primitives/VertexBillboard.cs index a864eada4..2c057d167 100644 --- a/Engine/Common/VertexBillboard.cs +++ b/Engine/BuiltIn/Primitives/VertexBillboard.cs @@ -1,7 +1,9 @@ using System.Runtime.InteropServices; -namespace Engine.Common +namespace Engine.BuiltIn.Primitives { + using Engine; + using Engine.Common; using SharpDX; using SharpDX.Direct3D11; diff --git a/Engine/Common/VertexCPUParticle.cs b/Engine/BuiltIn/Primitives/VertexCPUParticle.cs similarity index 97% rename from Engine/Common/VertexCPUParticle.cs rename to Engine/BuiltIn/Primitives/VertexCPUParticle.cs index 86a5f2dc8..157fa5c62 100644 --- a/Engine/Common/VertexCPUParticle.cs +++ b/Engine/BuiltIn/Primitives/VertexCPUParticle.cs @@ -1,8 +1,10 @@ using SharpDX; using System.Runtime.InteropServices; -namespace Engine.Common +namespace Engine.BuiltIn.Primitives { + using Engine; + using Engine.Common; using SharpDX.Direct3D11; /// diff --git a/Engine/Common/VertexDecal.cs b/Engine/BuiltIn/Primitives/VertexDecal.cs similarity index 98% rename from Engine/Common/VertexDecal.cs rename to Engine/BuiltIn/Primitives/VertexDecal.cs index aed681c96..03f9c88d6 100644 --- a/Engine/Common/VertexDecal.cs +++ b/Engine/BuiltIn/Primitives/VertexDecal.cs @@ -1,8 +1,10 @@ using SharpDX; using System.Runtime.InteropServices; -namespace Engine.Common +namespace Engine.BuiltIn.Primitives { + using Engine; + using Engine.Common; using SharpDX.Direct3D11; /// diff --git a/Engine/Common/VertexFont.cs b/Engine/BuiltIn/Primitives/VertexFont.cs similarity index 98% rename from Engine/Common/VertexFont.cs rename to Engine/BuiltIn/Primitives/VertexFont.cs index 3ca2fbdf4..4917420bf 100644 --- a/Engine/Common/VertexFont.cs +++ b/Engine/BuiltIn/Primitives/VertexFont.cs @@ -4,8 +4,10 @@ using System.Linq; using System.Runtime.InteropServices; -namespace Engine.Common +namespace Engine.BuiltIn.Primitives { + using Engine; + using Engine.Common; using SharpDX.Direct3D11; /// diff --git a/Engine/Common/VertexGPUParticle.cs b/Engine/BuiltIn/Primitives/VertexGPUParticle.cs similarity index 98% rename from Engine/Common/VertexGPUParticle.cs rename to Engine/BuiltIn/Primitives/VertexGPUParticle.cs index 9a0fc5a64..fac7fa2e4 100644 --- a/Engine/Common/VertexGPUParticle.cs +++ b/Engine/BuiltIn/Primitives/VertexGPUParticle.cs @@ -1,8 +1,10 @@ using SharpDX; using System.Runtime.InteropServices; -namespace Engine.Common +namespace Engine.BuiltIn.Primitives { + using Engine; + using Engine.Common; using SharpDX.Direct3D11; /// diff --git a/Engine/Common/VertexInstancingData.cs b/Engine/BuiltIn/Primitives/VertexInstancingData.cs similarity index 98% rename from Engine/Common/VertexInstancingData.cs rename to Engine/BuiltIn/Primitives/VertexInstancingData.cs index 86369a938..c5678afc5 100644 --- a/Engine/Common/VertexInstancingData.cs +++ b/Engine/BuiltIn/Primitives/VertexInstancingData.cs @@ -2,8 +2,9 @@ using SharpDX.DXGI; using System.Runtime.InteropServices; -namespace Engine.Common +namespace Engine.BuiltIn.Primitives { + using Engine; using SharpDX.Direct3D11; /// diff --git a/Engine/Common/VertexPosition.cs b/Engine/BuiltIn/Primitives/VertexPosition.cs similarity index 97% rename from Engine/Common/VertexPosition.cs rename to Engine/BuiltIn/Primitives/VertexPosition.cs index baeb3d282..945920798 100644 --- a/Engine/Common/VertexPosition.cs +++ b/Engine/BuiltIn/Primitives/VertexPosition.cs @@ -4,8 +4,10 @@ using System.Runtime.InteropServices; using System.Threading.Tasks; -namespace Engine.Common +namespace Engine.BuiltIn.Primitives { + using Engine; + using Engine.Common; using SharpDX.Direct3D11; /// @@ -129,7 +131,7 @@ public readonly int GetStride() /// Returns input elements public readonly InputElement[] GetInput(int slot) { - return VertexPosition.Input(slot); + return Input(slot); } /// diff --git a/Engine/Common/VertexPositionColor.cs b/Engine/BuiltIn/Primitives/VertexPositionColor.cs similarity index 98% rename from Engine/Common/VertexPositionColor.cs rename to Engine/BuiltIn/Primitives/VertexPositionColor.cs index af5019b3e..b03b6553a 100644 --- a/Engine/Common/VertexPositionColor.cs +++ b/Engine/BuiltIn/Primitives/VertexPositionColor.cs @@ -5,8 +5,10 @@ using System.Runtime.InteropServices; using System.Threading.Tasks; -namespace Engine.Common +namespace Engine.BuiltIn.Primitives { + using Engine; + using Engine.Common; using SharpDX.Direct3D11; /// diff --git a/Engine/Common/VertexPositionNormalColor.cs b/Engine/BuiltIn/Primitives/VertexPositionNormalColor.cs similarity index 98% rename from Engine/Common/VertexPositionNormalColor.cs rename to Engine/BuiltIn/Primitives/VertexPositionNormalColor.cs index d947f9045..a3a241300 100644 --- a/Engine/Common/VertexPositionNormalColor.cs +++ b/Engine/BuiltIn/Primitives/VertexPositionNormalColor.cs @@ -5,8 +5,10 @@ using System.Runtime.InteropServices; using System.Threading.Tasks; -namespace Engine.Common +namespace Engine.BuiltIn.Primitives { + using Engine; + using Engine.Common; using SharpDX.Direct3D11; /// diff --git a/Engine/Common/VertexPositionNormalTexture.cs b/Engine/BuiltIn/Primitives/VertexPositionNormalTexture.cs similarity index 98% rename from Engine/Common/VertexPositionNormalTexture.cs rename to Engine/BuiltIn/Primitives/VertexPositionNormalTexture.cs index ebccc298a..1338ed331 100644 --- a/Engine/Common/VertexPositionNormalTexture.cs +++ b/Engine/BuiltIn/Primitives/VertexPositionNormalTexture.cs @@ -5,8 +5,10 @@ using System.Runtime.InteropServices; using System.Threading.Tasks; -namespace Engine.Common +namespace Engine.BuiltIn.Primitives { + using Engine; + using Engine.Common; using SharpDX.Direct3D11; /// diff --git a/Engine/Common/VertexPositionNormalTextureTangent.cs b/Engine/BuiltIn/Primitives/VertexPositionNormalTextureTangent.cs similarity index 98% rename from Engine/Common/VertexPositionNormalTextureTangent.cs rename to Engine/BuiltIn/Primitives/VertexPositionNormalTextureTangent.cs index 71bf667ee..a7993b764 100644 --- a/Engine/Common/VertexPositionNormalTextureTangent.cs +++ b/Engine/BuiltIn/Primitives/VertexPositionNormalTextureTangent.cs @@ -5,8 +5,10 @@ using System.Runtime.InteropServices; using System.Threading.Tasks; -namespace Engine.Common +namespace Engine.BuiltIn.Primitives { + using Engine; + using Engine.Common; using SharpDX.Direct3D11; /// diff --git a/Engine/Common/VertexPositionTexture.cs b/Engine/BuiltIn/Primitives/VertexPositionTexture.cs similarity index 98% rename from Engine/Common/VertexPositionTexture.cs rename to Engine/BuiltIn/Primitives/VertexPositionTexture.cs index 04c7662f6..e0c4d4502 100644 --- a/Engine/Common/VertexPositionTexture.cs +++ b/Engine/BuiltIn/Primitives/VertexPositionTexture.cs @@ -5,8 +5,10 @@ using System.Runtime.InteropServices; using System.Threading.Tasks; -namespace Engine.Common +namespace Engine.BuiltIn.Primitives { + using Engine; + using Engine.Common; using SharpDX.Direct3D11; /// diff --git a/Engine/Common/VertexSkinnedPosition.cs b/Engine/BuiltIn/Primitives/VertexSkinnedPosition.cs similarity index 97% rename from Engine/Common/VertexSkinnedPosition.cs rename to Engine/BuiltIn/Primitives/VertexSkinnedPosition.cs index 2f26e017f..c7e9f5a35 100644 --- a/Engine/Common/VertexSkinnedPosition.cs +++ b/Engine/BuiltIn/Primitives/VertexSkinnedPosition.cs @@ -5,8 +5,10 @@ using System.Runtime.InteropServices; using System.Threading.Tasks; -namespace Engine.Common +namespace Engine.BuiltIn.Primitives { + using Engine; + using Engine.Common; using SharpDX.Direct3D11; /// @@ -139,7 +141,7 @@ public readonly bool HasChannel(VertexDataChannels channel) public readonly T GetChannelValue(VertexDataChannels channel) { if (channel == VertexDataChannels.Position) return (T)(object)Position; - else if (channel == VertexDataChannels.Weights) return (T)(object)(new[] { Weight1, Weight2, Weight3, (1.0f - Weight1 - Weight2 - Weight3) }); + else if (channel == VertexDataChannels.Weights) return (T)(object)(new[] { Weight1, Weight2, Weight3, 1.0f - Weight1 - Weight2 - Weight3 }); else if (channel == VertexDataChannels.BoneIndices) return (T)(object)(new[] { BoneIndex1, BoneIndex2, BoneIndex3, BoneIndex4 }); else throw new EngineException($"Channel data not found: {channel}"); } diff --git a/Engine/Common/VertexSkinnedPositionColor.cs b/Engine/BuiltIn/Primitives/VertexSkinnedPositionColor.cs similarity index 97% rename from Engine/Common/VertexSkinnedPositionColor.cs rename to Engine/BuiltIn/Primitives/VertexSkinnedPositionColor.cs index 1b23d065f..feadc7692 100644 --- a/Engine/Common/VertexSkinnedPositionColor.cs +++ b/Engine/BuiltIn/Primitives/VertexSkinnedPositionColor.cs @@ -5,8 +5,10 @@ using System.Runtime.InteropServices; using System.Threading.Tasks; -namespace Engine.Common +namespace Engine.BuiltIn.Primitives { + using Engine; + using Engine.Common; using SharpDX.Direct3D11; /// @@ -148,7 +150,7 @@ public readonly T GetChannelValue(VertexDataChannels channel) { if (channel == VertexDataChannels.Position) return (T)(object)Position; else if (channel == VertexDataChannels.Color) return (T)(object)Color; - else if (channel == VertexDataChannels.Weights) return (T)(object)(new[] { Weight1, Weight2, Weight3, (1.0f - Weight1 - Weight2 - Weight3) }); + else if (channel == VertexDataChannels.Weights) return (T)(object)(new[] { Weight1, Weight2, Weight3, 1.0f - Weight1 - Weight2 - Weight3 }); else if (channel == VertexDataChannels.BoneIndices) return (T)(object)(new[] { BoneIndex1, BoneIndex2, BoneIndex3, BoneIndex4 }); else throw new EngineException($"Channel data not found: {channel}"); } diff --git a/Engine/Common/VertexSkinnedPositionNormalColor.cs b/Engine/BuiltIn/Primitives/VertexSkinnedPositionNormalColor.cs similarity index 97% rename from Engine/Common/VertexSkinnedPositionNormalColor.cs rename to Engine/BuiltIn/Primitives/VertexSkinnedPositionNormalColor.cs index c1c2db472..56e651c14 100644 --- a/Engine/Common/VertexSkinnedPositionNormalColor.cs +++ b/Engine/BuiltIn/Primitives/VertexSkinnedPositionNormalColor.cs @@ -5,8 +5,10 @@ using System.Runtime.InteropServices; using System.Threading.Tasks; -namespace Engine.Common +namespace Engine.BuiltIn.Primitives { + using Engine; + using Engine.Common; using SharpDX.Direct3D11; /// @@ -157,7 +159,7 @@ public readonly T GetChannelValue(VertexDataChannels channel) if (channel == VertexDataChannels.Position) return (T)(object)Position; else if (channel == VertexDataChannels.Normal) return (T)(object)Normal; else if (channel == VertexDataChannels.Color) return (T)(object)Color; - else if (channel == VertexDataChannels.Weights) return (T)(object)(new[] { Weight1, Weight2, Weight3, (1.0f - Weight1 - Weight2 - Weight3) }); + else if (channel == VertexDataChannels.Weights) return (T)(object)(new[] { Weight1, Weight2, Weight3, 1.0f - Weight1 - Weight2 - Weight3 }); else if (channel == VertexDataChannels.BoneIndices) return (T)(object)(new[] { BoneIndex1, BoneIndex2, BoneIndex3, BoneIndex4 }); else throw new EngineException($"Channel data not found: {channel}"); } diff --git a/Engine/Common/VertexSkinnedPositionNormalTexture.cs b/Engine/BuiltIn/Primitives/VertexSkinnedPositionNormalTexture.cs similarity index 98% rename from Engine/Common/VertexSkinnedPositionNormalTexture.cs rename to Engine/BuiltIn/Primitives/VertexSkinnedPositionNormalTexture.cs index c581292c7..5d7b5dde2 100644 --- a/Engine/Common/VertexSkinnedPositionNormalTexture.cs +++ b/Engine/BuiltIn/Primitives/VertexSkinnedPositionNormalTexture.cs @@ -5,8 +5,10 @@ using System.Runtime.InteropServices; using System.Threading.Tasks; -namespace Engine.Common +namespace Engine.BuiltIn.Primitives { + using Engine; + using Engine.Common; using SharpDX.Direct3D11; /// @@ -157,7 +159,7 @@ public readonly T GetChannelValue(VertexDataChannels channel) if (channel == VertexDataChannels.Position) return (T)(object)Position; else if (channel == VertexDataChannels.Normal) return (T)(object)Normal; else if (channel == VertexDataChannels.Texture) return (T)(object)Texture; - else if (channel == VertexDataChannels.Weights) return (T)(object)(new[] { Weight1, Weight2, Weight3, (1.0f - Weight1 - Weight2 - Weight3) }); + else if (channel == VertexDataChannels.Weights) return (T)(object)(new[] { Weight1, Weight2, Weight3, 1.0f - Weight1 - Weight2 - Weight3 }); else if (channel == VertexDataChannels.BoneIndices) return (T)(object)(new[] { BoneIndex1, BoneIndex2, BoneIndex3, BoneIndex4 }); else throw new EngineException($"Channel data not found: {channel}"); } diff --git a/Engine/Common/VertexSkinnedPositionNormalTextureTangent.cs b/Engine/BuiltIn/Primitives/VertexSkinnedPositionNormalTextureTangent.cs similarity index 98% rename from Engine/Common/VertexSkinnedPositionNormalTextureTangent.cs rename to Engine/BuiltIn/Primitives/VertexSkinnedPositionNormalTextureTangent.cs index 86d0edb83..6f4f5da7a 100644 --- a/Engine/Common/VertexSkinnedPositionNormalTextureTangent.cs +++ b/Engine/BuiltIn/Primitives/VertexSkinnedPositionNormalTextureTangent.cs @@ -5,8 +5,10 @@ using System.Runtime.InteropServices; using System.Threading.Tasks; -namespace Engine.Common +namespace Engine.BuiltIn.Primitives { + using Engine; + using Engine.Common; using SharpDX.Direct3D11; /// @@ -166,7 +168,7 @@ public readonly T GetChannelValue(VertexDataChannels channel) else if (channel == VertexDataChannels.Normal) return (T)(object)Normal; else if (channel == VertexDataChannels.Texture) return (T)(object)Texture; else if (channel == VertexDataChannels.Tangent) return (T)(object)Tangent; - else if (channel == VertexDataChannels.Weights) return (T)(object)(new[] { Weight1, Weight2, Weight3, (1.0f - Weight1 - Weight2 - Weight3) }); + else if (channel == VertexDataChannels.Weights) return (T)(object)(new[] { Weight1, Weight2, Weight3, 1.0f - Weight1 - Weight2 - Weight3 }); else if (channel == VertexDataChannels.BoneIndices) return (T)(object)(new[] { BoneIndex1, BoneIndex2, BoneIndex3, BoneIndex4 }); else throw new EngineException($"Channel data not found: {channel}"); } diff --git a/Engine/Common/VertexSkinnedPositionTexture.cs b/Engine/BuiltIn/Primitives/VertexSkinnedPositionTexture.cs similarity index 97% rename from Engine/Common/VertexSkinnedPositionTexture.cs rename to Engine/BuiltIn/Primitives/VertexSkinnedPositionTexture.cs index 801968de4..dd3b0701e 100644 --- a/Engine/Common/VertexSkinnedPositionTexture.cs +++ b/Engine/BuiltIn/Primitives/VertexSkinnedPositionTexture.cs @@ -5,8 +5,10 @@ using System.Runtime.InteropServices; using System.Threading.Tasks; -namespace Engine.Common +namespace Engine.BuiltIn.Primitives { + using Engine; + using Engine.Common; using SharpDX.Direct3D11; /// @@ -148,7 +150,7 @@ public readonly T GetChannelValue(VertexDataChannels channel) { if (channel == VertexDataChannels.Position) return (T)(object)Position; else if (channel == VertexDataChannels.Texture) return (T)(object)Texture; - else if (channel == VertexDataChannels.Weights) return (T)(object)(new[] { Weight1, Weight2, Weight3, (1.0f - Weight1 - Weight2 - Weight3) }); + else if (channel == VertexDataChannels.Weights) return (T)(object)(new[] { Weight1, Weight2, Weight3, 1.0f - Weight1 - Weight2 - Weight3 }); else if (channel == VertexDataChannels.BoneIndices) return (T)(object)(new[] { BoneIndex1, BoneIndex2, BoneIndex3, BoneIndex4 }); else throw new EngineException($"Channel data not found: {channel}"); } diff --git a/Engine/Common/VertexTerrain.cs b/Engine/BuiltIn/Primitives/VertexTerrain.cs similarity index 98% rename from Engine/Common/VertexTerrain.cs rename to Engine/BuiltIn/Primitives/VertexTerrain.cs index 894e3de33..eb4c72d2b 100644 --- a/Engine/Common/VertexTerrain.cs +++ b/Engine/BuiltIn/Primitives/VertexTerrain.cs @@ -4,8 +4,10 @@ using System.Runtime.InteropServices; using System.Threading.Tasks; -namespace Engine.Common +namespace Engine.BuiltIn.Primitives { + using Engine; + using Engine.Common; using SharpDX.Direct3D11; /// diff --git a/Engine/Common/VertexTypes.cs b/Engine/BuiltIn/Primitives/VertexTypes.cs similarity index 98% rename from Engine/Common/VertexTypes.cs rename to Engine/BuiltIn/Primitives/VertexTypes.cs index c91ee58c1..29f0dec07 100644 --- a/Engine/Common/VertexTypes.cs +++ b/Engine/BuiltIn/Primitives/VertexTypes.cs @@ -1,5 +1,4 @@ - -namespace Engine.Common +namespace Engine.BuiltIn.Primitives { /// /// Vertext types enumeration diff --git a/Engine/Common/BaseModel.cs b/Engine/Common/BaseModel.cs index 600fb507e..32ba5663a 100644 --- a/Engine/Common/BaseModel.cs +++ b/Engine/Common/BaseModel.cs @@ -5,9 +5,6 @@ namespace Engine.Common { - using Engine.BuiltIn.Drawers; - using Engine.BuiltIn.Drawers.Deferred; - using Engine.BuiltIn.Drawers.Forward; using Engine.Content; /// @@ -278,27 +275,6 @@ public IEnumerable GetDrawingDataCollection() return meshesByLOD.Values.AsEnumerable(); } - /// - /// Gets the drawing effect for the current instance - /// - /// Drawing mode - /// Vertex type - /// Returns the drawing effect - protected IBuiltInDrawer GetDrawer(DrawerModes mode, VertexTypes vertexType, bool instanced) - { - if (mode.HasFlag(DrawerModes.Forward)) - { - return ForwardDrawerManager.GetDrawer(vertexType, instanced); - } - - if (mode.HasFlag(DrawerModes.Deferred)) - { - return DeferredDrawerManager.GetDrawer(vertexType, instanced); - } - - return null; - } - /// public IEnumerable GetMaterials() { diff --git a/Engine/Common/BufferDescriptorRequestInstancing.cs b/Engine/Common/BufferDescriptorRequestInstancing.cs index 002a3b6fe..360a5661f 100644 --- a/Engine/Common/BufferDescriptorRequestInstancing.cs +++ b/Engine/Common/BufferDescriptorRequestInstancing.cs @@ -4,7 +4,7 @@ namespace Engine.Common /// /// Instace buffer descriptor request /// - class BufferDescriptorRequestInstancing : IBufferDescriptorRequest + class BufferDescriptorRequestInstancing : IBufferDescriptorRequest where T : struct, IInstacingData { /// public string Id { get; set; } @@ -45,19 +45,19 @@ public void Process(BufferManager bufferManager) /// Buffer request private void Add(BufferManager bufferManager) { - BufferManagerInstances descriptor; + BufferManagerInstances descriptor; - Logger.WriteTrace(this, $"Add BufferDescriptor {(Dynamic ? "dynamic" : "static")} {typeof(VertexInstancingData)} [{Id}]"); + Logger.WriteTrace(this, $"Add BufferDescriptor {(Dynamic ? "dynamic" : "static")} {typeof(T)} [{Id}]"); var slot = bufferManager.FindInstancingBufferDescription(Dynamic); if (slot < 0) { - descriptor = new BufferManagerInstances(Dynamic); + descriptor = new BufferManagerInstances(Dynamic); slot = bufferManager.AddInstancingBufferDescription(descriptor); } else { - descriptor = bufferManager.GetInstancingBufferDescription(slot); + descriptor = bufferManager.GetInstancingBufferDescription(slot); descriptor.ReallocationNeeded = true; } @@ -71,9 +71,9 @@ private void Remove(BufferManager bufferManager) { if (Descriptor?.Ready == true) { - var descriptor = bufferManager.GetInstancingBufferDescription(Descriptor.BufferDescriptionIndex); + var descriptor = bufferManager.GetInstancingBufferDescription(Descriptor.BufferDescriptionIndex); - Logger.WriteTrace(this, $"Remove BufferDescriptor {(descriptor.Dynamic ? "dynamic" : "static")} {typeof(VertexInstancingData)} [{Descriptor.Id}]"); + Logger.WriteTrace(this, $"Remove BufferDescriptor {(descriptor.Dynamic ? "dynamic" : "static")} {typeof(T)} [{Descriptor.Id}]"); descriptor.RemoveDescriptor(Descriptor, Instances); descriptor.ReallocationNeeded = true; diff --git a/Engine/Common/BufferDescriptorRequestVertices.cs b/Engine/Common/BufferDescriptorRequestVertices.cs index 75601fced..4698d8952 100644 --- a/Engine/Common/BufferDescriptorRequestVertices.cs +++ b/Engine/Common/BufferDescriptorRequestVertices.cs @@ -1,5 +1,6 @@ using System.Collections.Generic; using System.Linq; +using Engine.BuiltIn.Primitives; namespace Engine.Common { diff --git a/Engine/Common/BufferManager.cs b/Engine/Common/BufferManager.cs index b9b0ab75a..fd204bcce 100644 --- a/Engine/Common/BufferManager.cs +++ b/Engine/Common/BufferManager.cs @@ -1,4 +1,5 @@ -using System; +using Engine.BuiltIn.Primitives; +using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq; @@ -68,7 +69,7 @@ struct InputAssemblerKey /// /// Instancing buffer descriptors /// - private readonly List> instancingBufferDescriptors = []; + private readonly List instancingBufferDescriptors = []; /// /// Input layouts by vertex shaders @@ -305,21 +306,15 @@ private static EngineBuffer CreateVertexBuffer(Graphics graphics, string name, b /// /// Creates an instancing buffer /// - /// Data type /// Graphics /// Buffer name + /// Instances + /// Instancing data stride in bytes /// Dynamic or Inmutable buffers - /// Instancing data /// Returns the new buffer - private static EngineBuffer CreateInstancingBuffer(Graphics graphics, string name, bool dynamic, IEnumerable instancingData) - where T : struct, IInstacingData + private static EngineBuffer CreateInstancingBuffer(Graphics graphics, string name, int instances, int stride, bool dynamic) { - if (instancingData?.Any() != true) - { - return null; - } - - return graphics.CreateVertexBuffer(name, instancingData, dynamic); + return graphics.CreateVertexBuffer(name, instances * stride, dynamic); } /// @@ -341,8 +336,6 @@ private void ReallocateInstances(string grId) foreach (var descriptor in dirtyList) { - var data = new VertexInstancingData[descriptor.Instances]; - if (descriptor.Allocated) { //Reserve current buffer @@ -350,7 +343,7 @@ private void ReallocateInstances(string grId) //Recreate the buffer and binding string name = GetInstancingBufferName(descriptor); - var buffer = CreateInstancingBuffer(game.Graphics, name, descriptor.Dynamic, data); + var buffer = CreateInstancingBuffer(game.Graphics, name, descriptor.Instances, descriptor.GetStride(), descriptor.Dynamic); vertexBuffers[descriptor.BufferIndex] = buffer; vertexBufferBindings[descriptor.BufferBindingIndex] = new(buffer, descriptor.GetStride(), 0); @@ -366,7 +359,7 @@ private void ReallocateInstances(string grId) //Create the buffer and binding string name = GetInstancingBufferName(descriptor); - var buffer = CreateInstancingBuffer(game.Graphics, name, descriptor.Dynamic, data); + var buffer = CreateInstancingBuffer(game.Graphics, name, descriptor.Instances, descriptor.GetStride(), descriptor.Dynamic); vertexBuffers.Add(buffer); vertexBufferBindings.Add(new(buffer, descriptor.GetStride(), 0)); @@ -497,7 +490,7 @@ private void ReallocateIndexData(string grId) /// Gets a buffer name for the specified descriptor /// /// Descriptor - private static string GetInstancingBufferName(BufferManagerInstances descriptor) + private static string GetInstancingBufferName(IEngineDescriptor descriptor) { return $"InstancingBuffer_v{descriptor.Allocations}.{descriptor.BufferIndex}.{(descriptor.Dynamic ? DynamicString : StaticString)}"; } @@ -543,14 +536,13 @@ public BufferManager Copy() for (int i = 0; i < instancingBufferDescriptors.Count; i++) { - var newDescriptor = (BufferManagerInstances)instancingBufferDescriptors[i].Copy(); + var newDescriptor = instancingBufferDescriptors[i].Copy(); bm.instancingBufferDescriptors.Add(newDescriptor); //Create the buffer and binding - var data = new VertexInstancingData[newDescriptor.Instances]; string name = GetInstancingBufferName(newDescriptor); - var buffer = CreateInstancingBuffer(game.Graphics, name, newDescriptor.Dynamic, data); + var buffer = CreateInstancingBuffer(game.Graphics, name, newDescriptor.Instances, newDescriptor.GetStride(), newDescriptor.Dynamic); tmpVBufferList[newDescriptor.BufferIndex] = buffer; tmpVBufferBindingsList[newDescriptor.BufferBindingIndex] = new(buffer, newDescriptor.GetStride(), 0); @@ -664,9 +656,9 @@ public EngineBuffer[] GetIndexBuffers() /// /// Gets instancing buffer descriptors /// - public IEngineBufferDescriptor[] GetInstancingBufferDescriptors() + public IEngineInstancingBufferDescriptor[] GetInstancingBufferDescriptors() { - return instancingBufferDescriptors.OfType().ToArray(); + return instancingBufferDescriptors.OfType().ToArray(); } /// /// Gets vertex buffer descriptors @@ -721,9 +713,9 @@ public bool GetOrCreateInputLayout(string name, IEngineShader vertexShader, Buff /// Id /// Add to dynamic buffers /// Number of instances - public BufferDescriptor AddInstancingData(string id, bool dynamic, int instances) + public BufferDescriptor AddInstancingData(string id, bool dynamic, int instances) where T : struct, IInstacingData { - var request = new BufferDescriptorRequestInstancing + var request = new BufferDescriptorRequestInstancing { Id = id, Dynamic = dynamic, @@ -804,14 +796,14 @@ public BufferDescriptor AddIndexData(string id, bool dynamic, IEnumerable /// Removes instancing data from buffer manager /// /// Buffer descriptor - public void RemoveInstancingData(BufferDescriptor descriptor) + public void RemoveInstancingData(BufferDescriptor descriptor) where T : struct, IInstacingData { if (descriptor == null) { return; } - var request = new BufferDescriptorRequestInstancing + var request = new BufferDescriptorRequestInstancing { Id = descriptor.Id, Descriptor = descriptor, @@ -866,7 +858,7 @@ public void RemoveIndexData(BufferDescriptor descriptor) /// /// Instancing buffer description /// Returns the internal description index - public int AddInstancingBufferDescription(BufferManagerInstances description) + public int AddInstancingBufferDescription(BufferManagerInstances description) where T : struct, IInstacingData { int index = instancingBufferDescriptors.Count; @@ -888,9 +880,9 @@ public int FindInstancingBufferDescription(bool dynamic) /// /// Index /// Returns the description - public BufferManagerInstances GetInstancingBufferDescription(int index) + public BufferManagerInstances GetInstancingBufferDescription(int index) where T : struct, IInstacingData { - return instancingBufferDescriptors[index]; + return instancingBufferDescriptors[index] as BufferManagerInstances; } /// diff --git a/Engine/Common/BufferManagerInstances.cs b/Engine/Common/BufferManagerInstances.cs index b743c5023..70c98f620 100644 --- a/Engine/Common/BufferManagerInstances.cs +++ b/Engine/Common/BufferManagerInstances.cs @@ -9,7 +9,7 @@ namespace Engine.Common /// /// Constructor /// - public class BufferManagerInstances(bool dynamic) : IEngineBufferDescriptor + public class BufferManagerInstances(bool dynamic) : IEngineInstancingBufferDescriptor where T : struct, IInstacingData { /// @@ -45,19 +45,12 @@ public bool Dirty return !Allocated || ReallocationNeeded; } } - /// - /// Instances - /// + /// public int Instances { get; set; } = 0; - /// - /// Vertex buffer binding index in the manager list - /// + /// public int BufferBindingIndex { get; set; } = -1; - /// - /// Gets the buffer format stride - /// - /// Returns the buffer format stride in bytes + /// public int GetStride() { return default(T).GetStride(); @@ -119,8 +112,9 @@ public void Allocate() Allocations++; ReallocationNeeded = false; } + /// - public IEngineBufferDescriptor Copy() + public IEngineInstancingBufferDescriptor Copy() { var d = new BufferManagerInstances(Dynamic) { diff --git a/Engine/Common/BufferManagerVertices.cs b/Engine/Common/BufferManagerVertices.cs index 3ed474278..25d250662 100644 --- a/Engine/Common/BufferManagerVertices.cs +++ b/Engine/Common/BufferManagerVertices.cs @@ -3,6 +3,7 @@ namespace Engine.Common { + using Engine.BuiltIn.Primitives; using SharpDX.Direct3D11; /// diff --git a/Engine/Common/IEngineBufferDescriptor.cs b/Engine/Common/IEngineBufferDescriptor.cs index 00f110d41..bd4b385b4 100644 --- a/Engine/Common/IEngineBufferDescriptor.cs +++ b/Engine/Common/IEngineBufferDescriptor.cs @@ -4,46 +4,8 @@ namespace Engine.Common /// /// Engine buffer descriptor interface /// - public interface IEngineBufferDescriptor + public interface IEngineBufferDescriptor : IEngineDescriptor { - /// - /// Dynamic buffer - /// - bool Dynamic { get; } - /// - /// Vertex buffer index in the buffer manager list - /// - int BufferIndex { get; } - /// - /// Gets whether the current buffer is dirty - /// - /// A buffer is dirty when needs reallocation or if it's not allocated at all - bool Dirty { get; } - /// - /// Allocated size into graphics device - /// - int AllocatedSize { get; } - /// - /// Gets the size of the data to allocate - /// - int ToAllocateSize { get; } - /// - /// Gets whether the internal buffer needs reallocation - /// - bool ReallocationNeeded { get; } - /// - /// Gets whether the internal buffer is currently allocated in the graphic device - /// - bool Allocated { get; } - /// - /// Number of allocations - /// - int Allocations { get; } - - /// - /// Updates the allocated buffer size - /// - void Allocate(); /// /// Copies the descriptor /// diff --git a/Engine/Common/IEngineDescriptor.cs b/Engine/Common/IEngineDescriptor.cs new file mode 100644 index 000000000..14b5ce24c --- /dev/null +++ b/Engine/Common/IEngineDescriptor.cs @@ -0,0 +1,48 @@ + +namespace Engine.Common +{ + /// + /// Engine descriptor interface + /// + public interface IEngineDescriptor + { + /// + /// Dynamic buffer + /// + bool Dynamic { get; } + /// + /// Vertex buffer index in the buffer manager list + /// + int BufferIndex { get; set; } + /// + /// Gets whether the current buffer is dirty + /// + /// A buffer is dirty when needs reallocation or if it's not allocated at all + bool Dirty { get; } + /// + /// Allocated size into graphics device + /// + int AllocatedSize { get; } + /// + /// Gets the size of the data to allocate + /// + int ToAllocateSize { get; } + /// + /// Gets whether the internal buffer needs reallocation + /// + bool ReallocationNeeded { get; } + /// + /// Gets whether the internal buffer is currently allocated in the graphic device + /// + bool Allocated { get; } + /// + /// Number of allocations + /// + int Allocations { get; } + + /// + /// Updates the allocated buffer size + /// + void Allocate(); + } +} diff --git a/Engine/Common/IEngineInstancingBufferDescriptor.cs b/Engine/Common/IEngineInstancingBufferDescriptor.cs new file mode 100644 index 000000000..1d65cd6f0 --- /dev/null +++ b/Engine/Common/IEngineInstancingBufferDescriptor.cs @@ -0,0 +1,29 @@ + +namespace Engine.Common +{ + /// + /// Engine instancing buffer descriptor interface + /// + public interface IEngineInstancingBufferDescriptor : IEngineDescriptor + { + /// + /// Instances + /// + int Instances { get; set; } + /// + /// Vertex buffer binding index in the manager list + /// + int BufferBindingIndex { get; set; } + + /// + /// Gets the buffer format stride + /// + /// Returns the buffer format stride in bytes + int GetStride(); + + /// + /// Copies the descriptor + /// + IEngineInstancingBufferDescriptor Copy(); + } +} diff --git a/Engine/Common/Mesh.cs b/Engine/Common/Mesh.cs index f8fd2c4df..379e0bb12 100644 --- a/Engine/Common/Mesh.cs +++ b/Engine/Common/Mesh.cs @@ -1,4 +1,5 @@ -using SharpDX; +using Engine.BuiltIn.Primitives; +using SharpDX; using System; using System.Collections.Generic; using System.Linq; diff --git a/Engine/Common/VertexData.cs b/Engine/Common/VertexData.cs index 8c9bd7c7b..16d677385 100644 --- a/Engine/Common/VertexData.cs +++ b/Engine/Common/VertexData.cs @@ -1,4 +1,5 @@ -using SharpDX; +using Engine.BuiltIn.Primitives; +using SharpDX; using System; using System.Collections.Generic; using System.Linq; diff --git a/Engine/Content/ContentData.cs b/Engine/Content/ContentData.cs index 0fa281032..cd3732c79 100644 --- a/Engine/Content/ContentData.cs +++ b/Engine/Content/ContentData.cs @@ -7,6 +7,7 @@ namespace Engine.Content { using Engine.Animation; + using Engine.BuiltIn.Primitives; using Engine.Common; using Engine.Content.Persistence; diff --git a/Engine/Content/SubMeshContent.cs b/Engine/Content/SubMeshContent.cs index 362f2a2bf..67ebbeab3 100644 --- a/Engine/Content/SubMeshContent.cs +++ b/Engine/Content/SubMeshContent.cs @@ -6,6 +6,7 @@ namespace Engine.Content { + using Engine.BuiltIn.Primitives; using Engine.Common; /// diff --git a/Engine/GameDrawingHelper.cs b/Engine/GameDrawingHelper.cs index ce9c983da..233f1b528 100644 --- a/Engine/GameDrawingHelper.cs +++ b/Engine/GameDrawingHelper.cs @@ -192,7 +192,7 @@ public static bool WriteIndexBuffer(this Game game, IEngineDeviceContext dc, Buf /// Buffer descriptors /// Buffer list /// Discards buffer content, no overwrite otherwise - private static bool WriteDiscardBuffer(this Game game, IEngineDeviceContext dc, BufferDescriptor descriptor, T[] data, IEngineBufferDescriptor[] bufferDescriptors, EngineBuffer[] buffers, bool discard) + private static bool WriteDiscardBuffer(this Game game, IEngineDeviceContext dc, BufferDescriptor descriptor, T[] data, IEngineDescriptor[] bufferDescriptors, EngineBuffer[] buffers, bool discard) where T : struct { if (!game.ValidateWriteBuffer(dc, descriptor, bufferDescriptors, buffers, out var buffer)) @@ -220,7 +220,7 @@ private static bool WriteDiscardBuffer(this Game game, IEngineDeviceContext d /// Buffer descriptors /// Buffer list /// Returns the buffer to update - private static bool ValidateWriteBuffer(this Game game, IEngineDeviceContext dc, BufferDescriptor descriptor, IEngineBufferDescriptor[] bufferDescriptors, EngineBuffer[] buffers, out EngineBuffer buffer) + private static bool ValidateWriteBuffer(this Game game, IEngineDeviceContext dc, BufferDescriptor descriptor, IEngineDescriptor[] bufferDescriptors, EngineBuffer[] buffers, out EngineBuffer buffer) { buffer = null; diff --git a/Engine/Graphics.Buffers.cs b/Engine/Graphics.Buffers.cs index 9ef924183..d58b3f3b8 100644 --- a/Engine/Graphics.Buffers.cs +++ b/Engine/Graphics.Buffers.cs @@ -5,6 +5,7 @@ namespace Engine { + using Engine.BuiltIn.Primitives; using Engine.Common; using SharpDX.Direct3D11; diff --git a/Engine/IShadowMap.cs b/Engine/IShadowMap.cs index b6f70b6ce..60d1c9356 100644 --- a/Engine/IShadowMap.cs +++ b/Engine/IShadowMap.cs @@ -3,6 +3,7 @@ namespace Engine { using Engine.BuiltIn.Drawers; + using Engine.BuiltIn.Primitives; using Engine.Common; /// diff --git a/Engine/IVertexData.cs b/Engine/IVertexData.cs index 258ab0dac..69866120a 100644 --- a/Engine/IVertexData.cs +++ b/Engine/IVertexData.cs @@ -1,6 +1,7 @@  namespace Engine { + using Engine.BuiltIn.Primitives; using Engine.Common; using SharpDX.Direct3D11; diff --git a/Engine/PostProcessingDrawer.cs b/Engine/PostProcessingDrawer.cs index 644167cc5..6fab8bb4e 100644 --- a/Engine/PostProcessingDrawer.cs +++ b/Engine/PostProcessingDrawer.cs @@ -3,6 +3,7 @@ namespace Engine { using Engine.BuiltIn.Drawers; using Engine.BuiltIn.Drawers.PostProcess; + using Engine.BuiltIn.Primitives; using Engine.Common; /// diff --git a/Engine/SceneRendererDeferredLights.cs b/Engine/SceneRendererDeferredLights.cs index 572664c5a..1a0b1f3fd 100644 --- a/Engine/SceneRendererDeferredLights.cs +++ b/Engine/SceneRendererDeferredLights.cs @@ -6,6 +6,7 @@ namespace Engine { using Engine.BuiltIn.Drawers; using Engine.BuiltIn.Drawers.Deferred; + using Engine.BuiltIn.Primitives; using Engine.Common; /// diff --git a/Engine/ShadowMap.cs b/Engine/ShadowMap.cs index f8b96f573..acd407220 100644 --- a/Engine/ShadowMap.cs +++ b/Engine/ShadowMap.cs @@ -7,6 +7,7 @@ namespace Engine { using Engine.BuiltIn.Drawers; using Engine.BuiltIn.Drawers.Shadows; + using Engine.BuiltIn.Primitives; using Engine.Common; /// diff --git a/Engine/UI/FontMapSentenceDescriptor.cs b/Engine/UI/FontMapSentenceDescriptor.cs index 98cb4f6e8..b90143675 100644 --- a/Engine/UI/FontMapSentenceDescriptor.cs +++ b/Engine/UI/FontMapSentenceDescriptor.cs @@ -4,7 +4,7 @@ namespace Engine.UI { - using Engine.Common; + using Engine.BuiltIn.Primitives; /// /// Sentence descriptor diff --git a/Engine/UI/FontMapWordDescriptor.cs b/Engine/UI/FontMapWordDescriptor.cs index ddb3f2724..d1cab3037 100644 --- a/Engine/UI/FontMapWordDescriptor.cs +++ b/Engine/UI/FontMapWordDescriptor.cs @@ -1,7 +1,7 @@  namespace Engine.UI { - using Engine.Common; + using Engine.BuiltIn.Primitives; /// /// Word descriptor diff --git a/Engine/UI/Sprite.cs b/Engine/UI/Sprite.cs index 32f6e97d2..025f4594d 100644 --- a/Engine/UI/Sprite.cs +++ b/Engine/UI/Sprite.cs @@ -5,6 +5,7 @@ namespace Engine.UI { using Engine.BuiltIn.Drawers; using Engine.BuiltIn.Drawers.Sprites; + using Engine.BuiltIn.Primitives; using Engine.Common; using Engine.Content; diff --git a/Engine/UI/UITextureRenderer.cs b/Engine/UI/UITextureRenderer.cs index d41f722c4..4143d8559 100644 --- a/Engine/UI/UITextureRenderer.cs +++ b/Engine/UI/UITextureRenderer.cs @@ -5,6 +5,7 @@ namespace Engine.UI { using Engine.BuiltIn.Drawers; using Engine.BuiltIn.Drawers.Sprites; + using Engine.BuiltIn.Primitives; using Engine.Common; using Engine.Content;