示例#1
0
        static void UpdateStorageBuffer(
            MgWriteDescriptorSet desc,
            IGLNextDescriptorPool pool,
            GLDescriptorPoolResourceInfo ticket,
            uint first,
            uint count,
            string errorParameterName
            )
        {
            if (ticket.GroupType == GLDescriptorBindingGroup.StorageBuffer)
            {
                for (var j = 0; j < count; j += 1)
                {
                    var info = desc.BufferInfo[j];
                    var buf  = (IGLBuffer)info.Buffer;

                    var isBufferFlags = MgBufferUsageFlagBits.STORAGE_BUFFER_BIT;

                    if (buf != null && ((buf.Usage & isBufferFlags) == isBufferFlags))
                    {
                        var index      = first + j;
                        var bufferDesc = pool.StorageBuffers.Items[index];
                        bufferDesc.BufferId = buf.BufferId;

                        if (info.Offset > (ulong)long.MaxValue)
                        {
                            throw new ArgumentOutOfRangeException(errorParameterName
                                                                  + ".BufferInfo[" + j + "].Offset is > long.MaxValue");
                        }

                        // CROSS PLATFORM ISSUE : VK_WHOLE_SIZE == ulong.MaxValue
                        if (info.Range == ulong.MaxValue)
                        {
                            throw new ArgumentOutOfRangeException(
                                      "Mg.OpenGL : Cannot accept " + errorParameterName
                                      + ".BufferInfo[" + j +
                                      "].Range == ulong.MaxValue (VK_WHOLE_SIZE). Please use actual size of buffer instead.");
                        }

                        if (info.Range > (ulong)int.MaxValue)
                        {
                            throw new ArgumentOutOfRangeException(
                                      errorParameterName
                                      + ".BufferInfo[" + j + "].Range is > int.MaxValue");
                        }

                        bufferDesc.Offset = (long)info.Offset;
                        // need to pass in whole
                        bufferDesc.Size      = (int)info.Range;
                        bufferDesc.IsDynamic = (desc.DescriptorType == MgDescriptorType.STORAGE_BUFFER_DYNAMIC);
                    }
                }
            }
        }
示例#2
0
        void UpdateCombinedImageSamplers(MgWriteDescriptorSet desc, IGLNextDescriptorPool parentPool,
                                         GLDescriptorPoolResourceInfo ticket, uint first, uint count)
        {
            if (ticket.GroupType == GLDescriptorBindingGroup.CombinedImageSampler)
            {
                for (var j = 0; j < count; j += 1)
                {
                    MgDescriptorImageInfo info = desc.ImageInfo[j];

                    var localSampler = (GLSampler)info.Sampler;
                    var localView    = (GLImageView)info.ImageView;

                    // Generate bindless texture handle
                    // FIXME : messy as F***

                    var texHandle = mImage.CreateHandle(localView.TextureId, localSampler.SamplerId);

                    var index = first + j;
                    parentPool.CombinedImageSamplers.Items[index].Replace(texHandle);
                }
            }
        }
示例#3
0
        // Allocate one region of memory for the uniform buffer
        private void InitializeUniforms()
        {
            MgBufferCreateInfo pCreateInfo = new MgBufferCreateInfo
            {
                Usage = MgBufferUsageFlagBits.UNIFORM_BUFFER_BIT,
                Size  = MaxBytesPerFrame,
            };
            IMgBuffer buffer;
            var       err = mConfiguration.Device.CreateBuffer(pCreateInfo, null, out buffer);

            Debug.Assert(err == Result.SUCCESS);
            //dynamicConstantBuffer = device.CreateBuffer(MaxBytesPerFrame, (MTLResourceOptions)0);
            //dynamicConstantBuffer.Label = "UniformBuffer";

            MgMemoryRequirements uniformsMemReqs;

            mConfiguration.Device.GetBufferMemoryRequirements(buffer, out uniformsMemReqs);

            const MgMemoryPropertyFlagBits uniformPropertyFlags = MgMemoryPropertyFlagBits.HOST_COHERENT_BIT;

            uint uniformMemoryTypeIndex;

            mConfiguration.Partition.GetMemoryType(
                uniformsMemReqs.MemoryTypeBits, uniformPropertyFlags, out uniformMemoryTypeIndex);

            var uniformMemAlloc = new MgMemoryAllocateInfo
            {
                MemoryTypeIndex = uniformMemoryTypeIndex,
                AllocationSize  = uniformsMemReqs.Size,
            };

            IMgDeviceMemory deviceMemory;
            var             result = mConfiguration.Device.AllocateMemory(uniformMemAlloc, null, out deviceMemory);

            Debug.Assert(result == Result.SUCCESS);

            buffer.BindBufferMemory(mConfiguration.Device, deviceMemory, 0);

            mUniforms = new BufferInfo
            {
                Buffer       = buffer,
                DeviceMemory = deviceMemory,
                Offset       = 0,
                Length       = MaxBytesPerFrame,
            };

            IMgDescriptorSetLayout pSetLayout;
            var dslCreateInfo = new MgDescriptorSetLayoutCreateInfo
            {
                Bindings = new MgDescriptorSetLayoutBinding[]
                {
                    new MgDescriptorSetLayoutBinding
                    {
                        Binding         = 0,
                        DescriptorCount = 1,
                        DescriptorType  = MgDescriptorType.UNIFORM_BUFFER_DYNAMIC,
                        StageFlags      = MgShaderStageFlagBits.VERTEX_BIT,
                    },
                },
            };

            err = mConfiguration.Device.CreateDescriptorSetLayout(dslCreateInfo, null, out pSetLayout);

            var poolCreateInfo = new Magnesium.MgDescriptorPoolCreateInfo
            {
                MaxSets   = 1,
                PoolSizes = new MgDescriptorPoolSize[] {
                    new MgDescriptorPoolSize
                    {
                        DescriptorCount = 1,
                        Type            = MgDescriptorType.COMBINED_IMAGE_SAMPLER,
                    },
                },
            };

            err = mConfiguration.Device.CreateDescriptorPool(poolCreateInfo, null, out mDescriptorPool);

            IMgDescriptorSet[]          dSets;
            MgDescriptorSetAllocateInfo pAllocateInfo = new MgDescriptorSetAllocateInfo
            {
                DescriptorPool     = mDescriptorPool,
                DescriptorSetCount = 1,
                SetLayouts         = new IMgDescriptorSetLayout[]
                {
                    pSetLayout,
                },
            };

            mConfiguration.Device.AllocateDescriptorSets(pAllocateInfo, out dSets);
            mUniformDescriptorSet = dSets[0];

            MgWriteDescriptorSet[] writes = new MgWriteDescriptorSet[]
            {
                new MgWriteDescriptorSet
                {
                    DescriptorCount = 1,
                    DescriptorType  = MgDescriptorType.UNIFORM_BUFFER_DYNAMIC,
                    DstSet          = mUniformDescriptorSet,
                    BufferInfo      = new MgDescriptorBufferInfo[]
                    {
                        new MgDescriptorBufferInfo
                        {
                            Buffer = mUniforms.Buffer,
                            Offset = mUniforms.Offset,
                            Range  = mUniforms.Length,
                        },
                    },
                    DstBinding = 0,
                }
            };
            mConfiguration.Device.UpdateDescriptorSets(writes, null);


            mSetLayout = pSetLayout;
        }