diff options
| author | Jean-Philippe Bruyère <jp_bruyere@hotmail.com> | 2019-08-30 10:46:13 +0200 |
|---|---|---|
| committer | Jean-Philippe Bruyère <jp_bruyere@hotmail.com> | 2019-08-30 10:46:13 +0200 |
| commit | 4a81ece67726666bd68698672d64247e0b1e3477 (patch) | |
| tree | 82e34f865d053b635e1732be105560bdbd07b37d | |
| parent | f406fb94272986db571ca10efdb0d10335809723 (diff) | |
uniformisation of Model derived classes, AddVertex with auto attribute configuration
| -rw-r--r-- | CVKL.Addons/gltfLoader/PbrModel.cs | 67 | ||||
| -rw-r--r-- | CVKL.Addons/gltfLoader/PbrModelSeparateTextures.cs | 26 | ||||
| -rw-r--r-- | CVKL.Addons/gltfLoader/PbrModelTexArray.cs | 35 | ||||
| -rw-r--r-- | CVKL/src/base/GraphicPipelineConfig.cs | 14 | ||||
| -rw-r--r-- | samples/Model/main.cs | 44 | ||||
| -rw-r--r-- | samples/deferred/DeferredPbrRenderer.cs | 3 | ||||
| -rw-r--r-- | samples/pbr/PbrModel.cs | 216 |
7 files changed, 95 insertions, 310 deletions
diff --git a/CVKL.Addons/gltfLoader/PbrModel.cs b/CVKL.Addons/gltfLoader/PbrModel.cs index c576d61..396c42f 100644 --- a/CVKL.Addons/gltfLoader/PbrModel.cs +++ b/CVKL.Addons/gltfLoader/PbrModel.cs @@ -11,50 +11,57 @@ using System.Collections.Generic; namespace CVKL.glTF { //TODO:stride in buffer views? public abstract class PbrModel : Model { - public new struct Vertex { - [VertexAttribute (VertexAttributeType.Position, VkFormat.R32g32b32Sfloat)] - public Vector3 pos; - [VertexAttribute (VertexAttributeType.Normal, VkFormat.R32g32b32Sfloat)] - public Vector3 normal; - [VertexAttribute (VertexAttributeType.UVs, VkFormat.R32g32Sfloat)] - public Vector2 uv0; - [VertexAttribute (VertexAttributeType.UVs, VkFormat.R32g32Sfloat)] - public Vector2 uv1; - public override string ToString () { - return pos.ToString () + ";" + normal.ToString () + ";" + uv0.ToString () + ";" + uv1.ToString (); - } - }; + //public new struct Vertex { + // [VertexAttribute (VertexAttributeType.Position, VkFormat.R32g32b32Sfloat)] + // public Vector3 pos; + // [VertexAttribute (VertexAttributeType.Normal, VkFormat.R32g32b32Sfloat)] + // public Vector3 normal; + // [VertexAttribute (VertexAttributeType.UVs, VkFormat.R32g32Sfloat)] + // public Vector2 uv0; + // [VertexAttribute (VertexAttributeType.UVs, VkFormat.R32g32Sfloat)] + // public Vector2 uv1; + // public override string ToString () { + // return pos.ToString () + ";" + normal.ToString () + ";" + uv0.ToString () + ";" + uv1.ToString (); + // } + //}; protected DescriptorPool descriptorPool; public GPUBuffer vbo; public GPUBuffer ibo; - public HostBuffer materialUBO; + public Buffer materialUBO; - public PbrModel (Queue transferQ, string path) { + protected PbrModel () { } + public PbrModel (Queue transferQ, string path) { dev = transferQ.Dev; using (CommandPool cmdPool = new CommandPool (dev, transferQ.index)) { using (glTFLoader ctx = new glTFLoader (path, transferQ, cmdPool)) { - ulong vertexCount, indexCount; + loadSolids (ctx); + } + } + } + + protected void loadSolids (glTFLoader ctx) { + loadSolids<Vertex> (ctx); + } - ctx.GetVertexCount (out vertexCount, out indexCount, out IndexBufferType); + protected void loadSolids<VX> (glTFLoader ctx) { + ulong vertexCount, indexCount; - ulong vertSize = vertexCount * (ulong)Marshal.SizeOf<Vertex> (); - ulong idxSize = indexCount * (IndexBufferType == VkIndexType.Uint16 ? 2ul : 4ul); - ulong size = vertSize + idxSize; + ctx.GetVertexCount (out vertexCount, out indexCount, out IndexBufferType); - vbo = new GPUBuffer (dev, VkBufferUsageFlags.VertexBuffer | VkBufferUsageFlags.TransferDst, vertSize); - ibo = new GPUBuffer (dev, VkBufferUsageFlags.IndexBuffer | VkBufferUsageFlags.TransferDst, idxSize); + ulong vertSize = vertexCount * (ulong)Marshal.SizeOf<VX> (); + ulong idxSize = indexCount * (IndexBufferType == VkIndexType.Uint16 ? 2ul : 4ul); + ulong size = vertSize + idxSize; - vbo.SetName ("vbo gltf"); - ibo.SetName ("ibo gltf"); + vbo = new GPUBuffer (dev, VkBufferUsageFlags.VertexBuffer | VkBufferUsageFlags.TransferDst, vertSize); + ibo = new GPUBuffer (dev, VkBufferUsageFlags.IndexBuffer | VkBufferUsageFlags.TransferDst, idxSize); - Meshes = new List<Mesh> (ctx.LoadMeshes<Vertex> (IndexBufferType, vbo, 0, ibo, 0)); - Scenes = new List<Scene> (ctx.LoadScenes (out defaultSceneIndex)); - } - } - } + vbo.SetName ("vbo gltf"); + ibo.SetName ("ibo gltf"); - protected PbrModel() { } + Meshes = new List<Mesh> (ctx.LoadMeshes<VX> (IndexBufferType, vbo, 0, ibo, 0)); + Scenes = new List<Scene> (ctx.LoadScenes (out defaultSceneIndex)); + } /// <summary> bind vertex and index buffers </summary> public virtual void Bind (CommandBuffer cmd) { diff --git a/CVKL.Addons/gltfLoader/PbrModelSeparateTextures.cs b/CVKL.Addons/gltfLoader/PbrModelSeparateTextures.cs index d5e7cbc..c1a6ee9 100644 --- a/CVKL.Addons/gltfLoader/PbrModelSeparateTextures.cs +++ b/CVKL.Addons/gltfLoader/PbrModelSeparateTextures.cs @@ -5,13 +5,9 @@ using System; using System.Diagnostics; using System.Numerics; using System.Runtime.InteropServices; - using VK; -using System.Collections.Generic; namespace CVKL.glTF { - using static VK.Utils; - /// <summary> /// Indexed pbr model whith one descriptorSet per material with separate textures attachments /// </summary> @@ -37,39 +33,25 @@ namespace CVKL.glTF { #pragma warning restore 169 } - Image[] textures; + protected Image[] textures; public Material[] materials; /// <summary> /// one descriptor per material containing textures /// </summary> - DescriptorSet[] descriptorSets; + protected DescriptorSet[] descriptorSets; + protected PbrModelSeparatedTextures () { } public PbrModelSeparatedTextures (Queue transferQ, string path, DescriptorSetLayout layout, params AttachmentType[] attachments) { dev = transferQ.Dev; using (CommandPool cmdPool = new CommandPool (dev, transferQ.index)) { using (glTFLoader ctx = new glTFLoader (path, transferQ, cmdPool)) { - ulong vertexCount, indexCount; - - ctx.GetVertexCount (out vertexCount, out indexCount, out IndexBufferType); - ulong vertSize = vertexCount * (ulong)Marshal.SizeOf<Vertex> (); - ulong idxSize = indexCount * (IndexBufferType == VkIndexType.Uint16 ? 2ul : 4ul); - ulong size = vertSize + idxSize; - - vbo = new GPUBuffer (dev, VkBufferUsageFlags.VertexBuffer | VkBufferUsageFlags.TransferDst, vertSize); - ibo = new GPUBuffer (dev, VkBufferUsageFlags.IndexBuffer | VkBufferUsageFlags.TransferDst, idxSize); - - vbo.SetName ("vbo gltf"); - ibo.SetName ("ibo gltf"); - - Meshes = new List<Mesh> (ctx.LoadMeshes<Vertex> (IndexBufferType, vbo, 0, ibo, 0)); + loadSolids (ctx); textures = ctx.LoadImages (); - loadMaterials (ctx, layout, attachments); materialUBO = new HostBuffer<Material> (dev, VkBufferUsageFlags.UniformBuffer, materials); - Scenes = new List<Scene> (ctx.LoadScenes (out defaultSceneIndex)); } } } diff --git a/CVKL.Addons/gltfLoader/PbrModelTexArray.cs b/CVKL.Addons/gltfLoader/PbrModelTexArray.cs index 97f6c14..5943774 100644 --- a/CVKL.Addons/gltfLoader/PbrModelTexArray.cs +++ b/CVKL.Addons/gltfLoader/PbrModelTexArray.cs @@ -4,18 +4,27 @@ using System.Diagnostics; using System.Numerics; using System.Runtime.InteropServices; - using VK; -using System.Collections.Generic; namespace CVKL.glTF { - using static VK.Utils; - /// <summary> /// Indexed pbr model whith one descriptorSet per material with separate textures attachments /// </summary> public class PbrModelTexArray : PbrModel { public static uint TEXTURE_DIM = 512; + public new struct Vertex { + [VertexAttribute (VertexAttributeType.Position, VkFormat.R32g32b32Sfloat)] + public Vector3 pos; + [VertexAttribute (VertexAttributeType.Normal, VkFormat.R32g32b32Sfloat)] + public Vector3 normal; + [VertexAttribute (VertexAttributeType.UVs, VkFormat.R32g32Sfloat)] + public Vector2 uv0; + [VertexAttribute (VertexAttributeType.UVs, VkFormat.R32g32Sfloat)] + public Vector2 uv1; + public override string ToString () { + return pos.ToString () + ";" + normal.ToString () + ";" + uv0.ToString () + ";" + uv1.ToString (); + } + }; /// <summary> /// Material structure for ubo containing texture indices in tex array @@ -49,20 +58,7 @@ namespace CVKL.glTF { dev = transferQ.Dev; using (CommandPool cmdPool = new CommandPool (dev, transferQ.index)) { using (glTFLoader ctx = new glTFLoader (path, transferQ, cmdPool)) { - ulong vertexCount, indexCount; - - ctx.GetVertexCount (out vertexCount, out indexCount, out IndexBufferType); - ulong vertSize = vertexCount * (ulong)Marshal.SizeOf<Vertex> (); - ulong idxSize = indexCount * (IndexBufferType == VkIndexType.Uint16 ? 2ul : 4ul); - ulong size = vertSize + idxSize; - - vbo = new GPUBuffer (dev, VkBufferUsageFlags.VertexBuffer | VkBufferUsageFlags.TransferDst, vertSize); - ibo = new GPUBuffer (dev, VkBufferUsageFlags.IndexBuffer | VkBufferUsageFlags.TransferDst, idxSize); - - vbo.SetName ("vbo gltf"); - ibo.SetName ("ibo gltf"); - - Meshes = new List<Mesh> (ctx.LoadMeshes<Vertex> (IndexBufferType, vbo, 0, ibo, 0)); + loadSolids<Vertex> (ctx); if (ctx.ImageCount > 0) { texArray = new Image (dev, Image.DefaultTextureFormat, VkImageUsageFlags.Sampled | VkImageUsageFlags.TransferDst | VkImageUsageFlags.TransferSrc, @@ -78,10 +74,7 @@ namespace CVKL.glTF { } loadMaterials (ctx); - materialUBO = new HostBuffer<Material> (dev, VkBufferUsageFlags.UniformBuffer, materials); - - Scenes = new List<Scene> (ctx.LoadScenes (out defaultSceneIndex)); } } } diff --git a/CVKL/src/base/GraphicPipelineConfig.cs b/CVKL/src/base/GraphicPipelineConfig.cs index 6ccd3f2..56b74ca 100644 --- a/CVKL/src/base/GraphicPipelineConfig.cs +++ b/CVKL/src/base/GraphicPipelineConfig.cs @@ -25,6 +25,7 @@ // THE SOFTWARE. using System; using System.Collections.Generic; +using System.Reflection; using System.Runtime.InteropServices; using VK; using static VK.Vk; @@ -112,9 +113,20 @@ namespace CVKL { public void AddVertexBinding (uint binding, uint stride, VkVertexInputRate inputRate = VkVertexInputRate.Vertex) { vertexBindings.Add (new VkVertexInputBindingDescription (binding, stride, inputRate)); } - public void AddVertexBinding<T> (uint binding, VkVertexInputRate inputRate = VkVertexInputRate.Vertex) { + public void AddVertexBinding<T> (uint binding = 0, VkVertexInputRate inputRate = VkVertexInputRate.Vertex) { vertexBindings.Add (new VkVertexInputBindingDescription (binding, (uint)Marshal.SizeOf<T> (), inputRate)); } + /// <summary> + /// Automatically configure Attribute for that binding. + /// </summary> + public void AddVertex<T> (uint binding = 0, VkVertexInputRate inputRate = VkVertexInputRate.Vertex) { + vertexBindings.Add (new VkVertexInputBindingDescription (binding, (uint)Marshal.SizeOf<T> (), inputRate)); + FieldInfo[] fields = typeof (T).GetFields (); + VkFormat[] attribs = new VkFormat[fields.Length]; + for (int i = 0; i < fields.Length; i++) + attribs[i] = fields[i].GetCustomAttribute<VertexAttributeAttribute> ().Format; + AddVertexAttributes (binding, attribs); + } public void AddShader (VkShaderStageFlags _stageFlags, string _spirvPath, SpecializationInfo specializationInfo = null, string _entryPoint = "main") { shaders.Add (new ShaderInfo (_stageFlags, _spirvPath, specializationInfo, _entryPoint)); } diff --git a/samples/Model/main.cs b/samples/Model/main.cs index b9b322c..f74b144 100644 --- a/samples/Model/main.cs +++ b/samples/Model/main.cs @@ -2,6 +2,7 @@ using System.Numerics; using System.Runtime.InteropServices; using CVKL; +using CVKL.glTF; using VK; namespace ModelSample { @@ -185,9 +186,18 @@ namespace ModelSample { buildCommandBuffers (); } - class SimpleModel : Model { - public GPUBuffer vbo; - public GPUBuffer ibo; + class SimpleModel : PbrModel { + public new struct Vertex { + [VertexAttribute (VertexAttributeType.Position, VkFormat.R32g32b32Sfloat)] + public Vector3 pos; + [VertexAttribute (VertexAttributeType.Normal, VkFormat.R32g32b32Sfloat)] + public Vector3 normal; + [VertexAttribute (VertexAttributeType.UVs, VkFormat.R32g32Sfloat)] + public Vector2 uv; + public override string ToString () { + return pos.ToString () + ";" + normal.ToString () + ";" + uv.ToString (); + } + }; public Image[] textures; public SimpleModel (Queue transferQ, string path) { @@ -195,32 +205,12 @@ namespace ModelSample { using (CommandPool cmdPool = new CommandPool (dev, transferQ.index)) { using (CVKL.glTF.glTFLoader ctx = new CVKL.glTF.glTFLoader(path, transferQ, cmdPool)) { - ulong vertexCount, indexCount; - - ctx.GetVertexCount (out vertexCount, out indexCount, out IndexBufferType); - - ulong vertSize = vertexCount * (ulong)Marshal.SizeOf<Vertex> (); - ulong idxSize = indexCount * (IndexBufferType == VkIndexType.Uint16 ? 2ul : 4ul); - ulong size = vertSize + idxSize; - - vbo = new GPUBuffer (dev, VkBufferUsageFlags.VertexBuffer | VkBufferUsageFlags.TransferDst, vertSize); - ibo = new GPUBuffer (dev, VkBufferUsageFlags.IndexBuffer | VkBufferUsageFlags.TransferDst, idxSize); - - vbo.SetName ("vbo gltf"); - ibo.SetName ("ibo gltf"); - - Meshes = new List<Mesh> (ctx.LoadMeshes<Vertex> (IndexBufferType, vbo, 0, ibo, 0)); - Scenes = new List<Scene> (ctx.LoadScenes (out defaultSceneIndex)); - + loadSolids<Vertex> (ctx); textures = ctx.LoadImages (); } } } - /// <summary> bind vertex and index buffers </summary> - public virtual void Bind (CommandBuffer cmd) { - cmd.BindVertexBuffer (vbo); - cmd.BindIndexBuffer (ibo, IndexBufferType); - } + public void DrawAll (CommandBuffer cmd, PipelineLayout pipelineLayout) { //helmet.Meshes cmd.BindVertexBuffer (vbo); @@ -236,6 +226,10 @@ namespace ModelSample { // RenderNode (cmd, pipelineLayout, node, sc.Root.localMatrix, shadowPass); //} } + + public override void RenderNode (CommandBuffer cmd, PipelineLayout pipelineLayout, Node node, Matrix4x4 currentTransform, bool shadowPass = false) { + throw new System.NotImplementedException (); + } } protected override void Dispose (bool disposing) { diff --git a/samples/deferred/DeferredPbrRenderer.cs b/samples/deferred/DeferredPbrRenderer.cs index fad2cbd..3dc8520 100644 --- a/samples/deferred/DeferredPbrRenderer.cs +++ b/samples/deferred/DeferredPbrRenderer.cs @@ -265,8 +265,7 @@ namespace deferred { cfg.blendAttachments.Add (new VkPipelineColorBlendAttachmentState (false)); //cfg.blendAttachments.Add (new VkPipelineColorBlendAttachmentState (false)); - cfg.AddVertexBinding<PbrModel.Vertex> (0); - cfg.AddVertexAttributes (0, VkFormat.R32g32b32Sfloat, VkFormat.R32g32b32Sfloat, VkFormat.R32g32Sfloat, VkFormat.R32g32Sfloat); + cfg.AddVertex<PbrModelTexArray.Vertex> (); using (SpecializationInfo constants = new SpecializationInfo ( new SpecializationConstant<float> (0, nearPlane), diff --git a/samples/pbr/PbrModel.cs b/samples/pbr/PbrModel.cs index 766ded5..a48fe11 100644 --- a/samples/pbr/PbrModel.cs +++ b/samples/pbr/PbrModel.cs @@ -1,201 +1,30 @@ // Copyright (c) 2019 Jean-Philippe Bruyère <jp_bruyere@hotmail.com> // // This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT) -using System; -using System.Diagnostics; using System.Numerics; using System.Runtime.InteropServices; using VK; -using System.Collections.Generic; namespace CVKL { - using static VK.Utils; + using CVKL.glTF; //TODO:stride in buffer views? - public class PbrModel2 : Model { - public new struct Vertex { - [VertexAttribute (VertexAttributeType.Position, VkFormat.R32g32b32Sfloat)] - public Vector3 pos; - [VertexAttribute (VertexAttributeType.Normal, VkFormat.R32g32b32Sfloat)] - public Vector3 normal; - [VertexAttribute (VertexAttributeType.UVs, VkFormat.R32g32Sfloat)] - public Vector2 uv0; - [VertexAttribute (VertexAttributeType.UVs, VkFormat.R32g32Sfloat)] - public Vector2 uv1; - public override string ToString () { - return pos.ToString () + ";" + normal.ToString () + ";" + uv0.ToString () + ";" + uv1.ToString (); - } - }; - /// <summary> - /// Pbr data structure suitable for push constant or ubo, containing - /// availablility of attached textures and the coef of pbr inputs - /// </summary> - public struct PbrMaterial { - public Vector4 baseColorFactor; - public Vector4 emissiveFactor; - public Vector4 diffuseFactor; - public Vector4 specularFactor; - public float workflow; - public AttachmentType TexCoord0; - public AttachmentType TexCoord1; - public float metallicFactor; - public float roughnessFactor; - public float alphaMask; - public float alphaMaskCutoff; - int pad0;//see std420 - } - - Image[] textures; - PbrMaterial[] materials; - DescriptorSet[] descriptorSets; - - DescriptorPool descriptorPool; - public GPUBuffer vbo; - public GPUBuffer ibo; - public GPUBuffer<PbrMaterial> materialUBO; - - public PbrModel2 (Queue transferQ, string path) { - dev = transferQ.Dev; - using (CommandPool cmdPool = new CommandPool (dev, transferQ.index)) { - using (glTF.glTFLoader ctx = new glTF.glTFLoader (path, transferQ, cmdPool)) { - ulong vertexCount, indexCount; - - ctx.GetVertexCount (out vertexCount, out indexCount, out IndexBufferType); - - ulong vertSize = vertexCount * (ulong)Marshal.SizeOf<Vertex> (); - ulong idxSize = indexCount * (IndexBufferType == VkIndexType.Uint16 ? 2ul : 4ul); - ulong size = vertSize + idxSize; - - vbo = new GPUBuffer (dev, VkBufferUsageFlags.VertexBuffer | VkBufferUsageFlags.TransferDst, vertSize); - ibo = new GPUBuffer (dev, VkBufferUsageFlags.IndexBuffer | VkBufferUsageFlags.TransferDst, idxSize); - - vbo.SetName ("vbo gltf"); - ibo.SetName ("ibo gltf"); - - Meshes = new List<Mesh> (ctx.LoadMeshes<Vertex> (IndexBufferType, vbo, 0, ibo, 0)); - Scenes = new List<Scene> (ctx.LoadScenes (out defaultSceneIndex)); - } - } - } - public PbrModel2 (Queue transferQ, string path, DescriptorSetLayout layout, params AttachmentType[] attachments) { - dev = transferQ.Dev; - using (CommandPool cmdPool = new CommandPool (dev, transferQ.index)) { - using (glTF.glTFLoader ctx = new glTF.glTFLoader (path, transferQ, cmdPool)) { - ulong vertexCount, indexCount; - - ctx.GetVertexCount (out vertexCount, out indexCount, out IndexBufferType); - ulong vertSize = vertexCount * (ulong)Marshal.SizeOf<Vertex> (); - ulong idxSize = indexCount * (IndexBufferType == VkIndexType.Uint16 ? 2ul : 4ul); - ulong size = vertSize + idxSize; - - vbo = new GPUBuffer (dev, VkBufferUsageFlags.VertexBuffer | VkBufferUsageFlags.TransferDst, vertSize); - ibo = new GPUBuffer (dev, VkBufferUsageFlags.IndexBuffer | VkBufferUsageFlags.TransferDst, idxSize); - - vbo.SetName ("vbo gltf"); - ibo.SetName ("ibo gltf"); - - Meshes = new List<Mesh> (ctx.LoadMeshes<Vertex> (IndexBufferType, vbo, 0, ibo, 0)); - textures = ctx.LoadImages (); - - loadMaterials (ctx, layout, attachments); - - materialUBO = new GPUBuffer<PbrMaterial> (transferQ, cmdPool, VkBufferUsageFlags.UniformBuffer, materials); - - Scenes = new List<Scene> (ctx.LoadScenes (out defaultSceneIndex)); - } - } - } - - void loadMaterials (glTF.glTFLoader ctx, DescriptorSetLayout layout, params AttachmentType[] attachments) { - glTF.glTFLoader.Material[] mats = ctx.LoadMaterial (); - materials = new PbrMaterial[mats.Length]; - descriptorSets = new DescriptorSet[mats.Length]; - - if (attachments.Length == 0) - throw new InvalidOperationException ("At least one attachment is required for Model.WriteMaterialDescriptor"); - - descriptorPool = new DescriptorPool (dev, (uint)materials.Length, - new VkDescriptorPoolSize (VkDescriptorType.CombinedImageSampler, (uint)(attachments.Length * materials.Length)) - ); - descriptorPool.SetName ("descPool gltfTextures"); - - for (int i = 0; i < mats.Length; i++) { - materials[i] = new PbrMaterial { - workflow = (float)mats[i].workflow, - baseColorFactor = mats[i].baseColorFactor, - emissiveFactor = mats[i].emissiveFactor, - metallicFactor = mats[i].metallicFactor, - roughnessFactor = mats[i].roughnessFactor, - TexCoord0 = mats[i].availableAttachments, - TexCoord1 = mats[i].availableAttachments1, - alphaMask = 0f, - alphaMaskCutoff = 0.0f, - diffuseFactor = new Vector4(0), - specularFactor = new Vector4 (0) - }; - - descriptorSets[i] = descriptorPool.Allocate (layout); - descriptorSets[i].Handle.SetDebugMarkerName (dev, "descSet " + mats[i].Name); - - VkDescriptorSetLayoutBinding dslb = - new VkDescriptorSetLayoutBinding (0, VkShaderStageFlags.Fragment, VkDescriptorType.CombinedImageSampler); - - using (DescriptorSetWrites2 uboUpdate = new DescriptorSetWrites2 (dev)) { - for (uint a = 0; a < attachments.Length; a++) { - dslb.binding = a; - switch (attachments[a]) { - case AttachmentType.None: - break; - case AttachmentType.Color: - if (mats[i].availableAttachments.HasFlag (AttachmentType.Color)) - uboUpdate.AddWriteInfo (descriptorSets[i], dslb, textures[(int)mats[i].baseColorTexture].Descriptor); - break; - case AttachmentType.Normal: - if (mats[i].availableAttachments.HasFlag (AttachmentType.Normal)) - uboUpdate.AddWriteInfo (descriptorSets[i], dslb, textures[(int)mats[i].normalTexture].Descriptor); - break; - case AttachmentType.AmbientOcclusion: - if (mats[i].availableAttachments.HasFlag (AttachmentType.AmbientOcclusion)) - uboUpdate.AddWriteInfo (descriptorSets[i], dslb, textures[(int)mats[i].occlusionTexture].Descriptor); - break; - case AttachmentType.PhysicalProps: - if (mats[i].availableAttachments.HasFlag (AttachmentType.PhysicalProps)) - uboUpdate.AddWriteInfo (descriptorSets[i], dslb, textures[(int)mats[i].metallicRoughnessTexture].Descriptor); - break; - case AttachmentType.Metal: - break; - case AttachmentType.Roughness: - break; - case AttachmentType.Emissive: - if (mats[i].availableAttachments.HasFlag (AttachmentType.Emissive)) - uboUpdate.AddWriteInfo (descriptorSets[i], dslb, textures[(int)mats[i].emissiveTexture].Descriptor); - break; - } - } - uboUpdate.Update (); - } - } - } - - - /// <summary> bind vertex and index buffers </summary> - public void Bind (CommandBuffer cmd) { - cmd.BindVertexBuffer (vbo); - cmd.BindIndexBuffer (ibo, IndexBufferType); - } + public class PbrModel2 : PbrModelSeparatedTextures { + public PbrModel2 (Queue transferQ, string path, DescriptorSetLayout layout, params AttachmentType[] attachments) + : base (transferQ, path, layout, attachments) {} //TODO:destset for binding must be variable //TODO: ADD REFAULT MAT IF NO MAT DEFINED - public void RenderNode (CommandBuffer cmd, PipelineLayout pipelineLayout, Node node, Matrix4x4 currentTransform) { + public override void RenderNode (CommandBuffer cmd, PipelineLayout pipelineLayout, Node node, Matrix4x4 currentTransform, bool shadowPass = false) { Matrix4x4 localMat = node.localMatrix * currentTransform; cmd.PushConstant (pipelineLayout, VkShaderStageFlags.Vertex, localMat); if (node.Mesh != null) { foreach (Primitive p in node.Mesh.Primitives) { - cmd.PushConstant (pipelineLayout, VkShaderStageFlags.Fragment, (int)p.material, (uint)Marshal.SizeOf<Matrix4x4>()); + cmd.PushConstant (pipelineLayout, VkShaderStageFlags.Fragment, (int)p.material, (uint)Marshal.SizeOf<Matrix4x4> ()); if (descriptorSets[p.material] != null) cmd.BindDescriptorSet (pipelineLayout, descriptorSets[p.material], 1); cmd.DrawIndexed (p.indexCount, 1, p.indexBase, p.vertexBase, 0); @@ -203,39 +32,8 @@ namespace CVKL { } if (node.Children == null) return; - foreach (Node child in node.Children) + foreach (Node child in node.Children) RenderNode (cmd, pipelineLayout, child, localMat); } - - public void DrawAll (CommandBuffer cmd, PipelineLayout pipelineLayout) { - foreach (Scene sc in Scenes) { - foreach (Node node in sc.Root.Children) { - RenderNode (cmd, pipelineLayout, node, sc.Root.localMatrix); - } - } - } - - #region IDisposable Support - private bool isDisposed = false; // Pour détecter les appels redondants - - protected virtual void Dispose (bool disposing) { - if (!isDisposed) { - if (disposing) { - ibo?.Dispose (); - vbo?.Dispose (); - materialUBO?.Dispose (); - descriptorPool?.Dispose (); - foreach (Image txt in textures) { - txt.Dispose (); - } - } else - Debug.WriteLine ("model was not disposed"); - isDisposed = true; - } - } - public void Dispose () { - Dispose (true); - } - #endregion } } |