Exemplo n.º 1
0
        void loadMaterials(glTFLoader ctx)
        {
            Model.Material[] mats = ctx.LoadMaterial();
            materials = new Material[mats.Length];

            for (int i = 0; i < mats.Length; i++)
            {
                materials[i] = new Material {
                    workflow        = (float)mats[i].workflow,
                    baseColorFactor = mats[i].baseColorFactor,
                    emissiveFactor  = mats[i].emissiveFactor,
                    metallicFactor  = mats[i].metallicFactor,
                    roughnessFactor = mats[i].roughnessFactor,

                    baseColorTextureSet = mats[i].baseColorTexture,
                    phyDescTex          = mats[i].metallicRoughnessTexture,
                    normalTex           = mats[i].normalTexture,
                    aoTex       = mats[i].occlusionTexture,
                    emissiveTex = mats[i].emissiveTexture,

                    TexCoord0 = mats[i].availableAttachments,
                    TexCoord1 = mats[i].availableAttachments1,

                    alphaMask       = 0f,
                    alphaMaskCutoff = 0.0f,
                    diffuseFactor   = new Vector4(0),
                    specularFactor  = new Vector4(0)
                };
            }
        }
Exemplo n.º 2
0
        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));

                    textures = ctx.LoadImages();

                    loadMaterials(ctx, layout, attachments);

                    materialUBO = new HostBuffer <Material> (dev, VkBufferUsageFlags.UniformBuffer, materials);

                    Scenes = new List <Scene> (ctx.LoadScenes(out defaultSceneIndex));
                }
            }
        }
Exemplo n.º 3
0
        public PbrModelTexArray(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;

                    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));

                    if (ctx.ImageCount > 0)
                    {
                        texArray = new Image(dev, Image.DefaultTextureFormat, VkImageUsageFlags.Sampled | VkImageUsageFlags.TransferDst | VkImageUsageFlags.TransferSrc,
                                             VkMemoryPropertyFlags.DeviceLocal, TEXTURE_DIM, TEXTURE_DIM, VkImageType.Image2D,
                                             VkSampleCountFlags.SampleCount1, VkImageTiling.Optimal, Image.ComputeMipLevels(TEXTURE_DIM), ctx.ImageCount);

                        ctx.BuildTexArray(ref texArray, 0);

                        texArray.CreateView(VkImageViewType.ImageView2DArray, VkImageAspectFlags.Color, texArray.CreateInfo.arrayLayers);
                        texArray.CreateSampler();
                        texArray.Descriptor.imageLayout = VkImageLayout.ShaderReadOnlyOptimal;
                        texArray.SetName("model texArray");
                    }

                    loadMaterials(ctx);

                    materialUBO = new HostBuffer <Material> (dev, VkBufferUsageFlags.UniformBuffer, materials);

                    Scenes = new List <Scene> (ctx.LoadScenes(out defaultSceneIndex));
                }
            }
        }
Exemplo n.º 4
0
        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;

                    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));
                }
            }
        }
Exemplo n.º 5
0
        void loadMaterials(glTFLoader ctx, DescriptorSetLayout layout, params AttachmentType[] attachments)
        {
            Model.Material[] mats = ctx.LoadMaterial();
            materials      = new Material[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 Material {
                    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();
                }
            }
        }