public static H1HeapType GetHeapTypeFromTexture2DDesc(H1Texture2D.Description desc)
        {
            H1HeapType result = H1HeapType.Default;

            if (desc.Usage == H1Usage.Staging)
            {
                result = H1HeapType.Readback;
            }

            else if (desc.Usage == H1Usage.Dynamic)
            {
                result = H1HeapType.Upload;
            }

            if (desc.CPUAccessFlags != 0)
            {
                if (desc.CPUAccessFlags == Convert.ToUInt32(H1CPUAccessFlag.Write))
                {
                    result = H1HeapType.Upload;
                }

                if (desc.CPUAccessFlags == Convert.ToUInt32(H1CPUAccessFlag.Write))
                {
                    result = H1HeapType.Readback;
                }
            }

            return(result);
        }
        // static method for more general creat page for texture resided in GPU memory
        protected virtual Boolean CreatePage(H1PixelFormat format, Int32 width, Int32 height, Int32 depthOrArraySize, H1OptionalParametersForTexture options = null)
        {
            // invalid call for create page
            if (Owner.PageType != H1GpuMemoryPageType.Normal)
            {
                return(false);
            }

            // create resource encapsulated GPU API layer
            H1HeapType       heapType = H1HeapType.Unknown;
            H1ResourceStates usage    = H1ResourceStates.Invalid;

            if (Owner.Type != H1GpuMemoryType.GpuWritable)
            {
                // invalid gpu memory type
                // CpuWritable type should be H1GpuMemoryPageSegmented type
                return(false);
            }

            // GPU-writable
            heapType = H1HeapType.Default;
            H1GpuResourceDesc resourceDesc = new H1GpuResourceDesc();

            resourceDesc.Alignment        = 0; // depends on GDI decision for choosing appropriate alignment
            resourceDesc.Width            = Convert.ToUInt32(width);
            resourceDesc.Height           = Convert.ToUInt32(height);
            resourceDesc.DepthOrArraySize = Convert.ToUInt16(depthOrArraySize);
            resourceDesc.Format           = format;

            // these description properties are default values
            resourceDesc.SampleDesc.Count   = 1;
            resourceDesc.SampleDesc.Quality = 0;
            resourceDesc.Layout             = H1TextureLayout.RowMajor;
            resourceDesc.MipLevels          = 1;

            // apply optional parameters
            if (options != null)
            {
                resourceDesc.Flags = options.ResourceFlags;
                usage = options.ResourceStates;

                // resource description optional parameters
                resourceDesc.MipLevels = options.MipLevels;
                resourceDesc.Layout    = options.Layout;

                // struct copy (shallow copy should be occurred)
                resourceDesc.SampleDesc = options.SampleDesc;
            }

            // create new page resource
            //if (!Resource.CreateResource(heapType, resourceDesc, usage))
            {
                return(false);
            }

            //return true;
        }
Exemple #3
0
        public static H1GpuResourceSegmented CreateGpuResource(H1HeapType heapType, H1GpuResourceDesc resourceDesc, H1ResourceStates defaultUsage)
        {
            H1GpuResourceSegmented newRes = new H1GpuResourceSegmented();

            if (!newRes.CreateResource(heapType, resourceDesc, defaultUsage))
            {
                newRes = null;
            }
            return(newRes);
        }
Exemple #4
0
        public static H1GpuResourceSingle CreateGpuResource(H1HeapType heapType, H1GpuResourceDesc resourceDesc, H1ResourceStates defaultUsage)
        {
            H1GpuResourceSingle newRes = new H1GpuResourceSingle();

            if (!newRes.CreateResource(heapType, resourceDesc, defaultUsage))
            {
                newRes = null; // nullify newly created resource
            }
            return(newRes);
        }
Exemple #5
0
        protected Boolean CreateResource(H1HeapType heapType, H1GpuResourceDesc resourceDesc, H1ResourceStates defaultUsage)
        {
            Int32 totalSizeInBytes = CreateResourcePlatformDependent(heapType, resourceDesc, defaultUsage);

            if (m_GpuResource == null)
            {
                return(false);
            }

            // based on created platform dependent resource information, create alloc policy
            RegisterAllocPolicy(totalSizeInBytes);

            return(true);
        }
        // generic version for page creation
        protected virtual Boolean CreatePage(Int64 sizeInBytes, H1OptionalParameters options = null)
        {
            // invalid call for create page
            if (Owner.PageType != H1GpuMemoryPageType.Normal)
            {
                return(false);
            }

            // create resource encapsulated GPU API layer
            H1HeapType       heapType = H1HeapType.Unknown;
            H1ResourceStates usage    = H1ResourceStates.Invalid;

            H1GpuResourceDesc resourceDesc = new H1GpuResourceDesc();

            resourceDesc.Alignment          = 0; // depends on GDI decision for choosing appropriate alignment
            resourceDesc.Height             = 1;
            resourceDesc.DepthOrArraySize   = 1;
            resourceDesc.MipLevels          = 1;
            resourceDesc.Format             = H1PixelFormat.Unknown;
            resourceDesc.SampleDesc.Count   = 1;
            resourceDesc.SampleDesc.Quality = 0;
            resourceDesc.Layout             = H1TextureLayout.RowMajor;

            if (Owner.Type != H1GpuMemoryType.GpuWritable)
            {
                // invalid gpu memory type
                // CpuWritable type should be H1GpuMemoryPageSegmented type
                return(false);
            }

            // GPU-writable
            heapType           = H1HeapType.Default;
            resourceDesc.Width = Convert.ToUInt32(sizeInBytes);

            // apply optional parameters
            if (options != null)
            {
                resourceDesc.Flags = options.ResourceFlags;
                usage = options.ResourceStates;
            }

            // create new page resource
            //if (!Resource.CreateResource(heapType, resourceDesc, usage))
            {
                return(false);
            }

            //return true;
        }
        static void CreatePlatformDependent(Vector4 clearValue, H1Texture2D.Description desc, H1SubresourceData initialData, ref H1GpuTexture2D result)
        {
            // get device for directX 12
            Device deviceDX12 = H1Global <H1ManagedRenderer> .Instance.Device;

            H1GpuResourceDesc desc12         = H1RHIDefinitionHelper.Texture2DDescToGpuResourceDesc(desc);
            H1HeapType        heapType       = H1RHIDefinitionHelper.GetHeapTypeFromTexture2DDesc(desc);
            H1ResourceStates  resourceStates = H1RHIDefinitionHelper.GetResourceStatesFromTexture2DDesc(desc);

            // generate resource
            //if (result != null)
            //    result.Resource.CreateResource(heapType, desc12, resourceStates);

            // generate RHI resource description (need resource description for generating UAV or SRV or etc.)
            result.CreateResourceDescription(desc12);
        }
        Int32 CreateResourcePlatformDependent(H1HeapType heapType, H1GpuResourceDesc resourceDesc, H1ResourceStates defaultUsage)
        {
            HeapProperties heapProps = new HeapProperties();

            heapProps.CPUPageProperty      = CpuPageProperty.Unknown;
            heapProps.MemoryPoolPreference = MemoryPool.Unknown;
            heapProps.CreationNodeMask     = 1;
            heapProps.VisibleNodeMask      = 1;

            // convert to H1HeapType to HeapType for dx12
            switch (heapType)
            {
            case H1HeapType.Default:
                heapProps.Type = HeapType.Default;
                break;

            case H1HeapType.Readback:
                heapProps.Type = HeapType.Readback;
                break;

            case H1HeapType.Upload:
                heapProps.Type = HeapType.Upload;
                break;
            }

            // convert H1ResourceDesc to ResourceDesc for dx12
            ResourceDescription resourceDescDX12 = ConvertToResourceDescDx12(resourceDesc);
            ResourceStates      defaultStates    = (ResourceStates)ResourceStateMapper[Convert.ToInt32(defaultUsage)];

            Device deviceDX11 = H1Global <H1ManagedRenderer> .Instance.Device;

            m_GpuResource = deviceDX11.CreateCommittedResource(heapProps, HeapFlags.None, resourceDescDX12, defaultStates);

            Int32 elementSize = Convert.ToInt32(H1RHIDefinitionHelper.ElementTypeToSize(resourceDesc.Format));

            if (elementSize == 0)
            {
                throw new InvalidOperationException("there is no appropriate format is implemented in ElementTypeToSize()!");
            }

            Int32 totalSizeInBytes = elementSize * Convert.ToInt32(resourceDesc.Width * resourceDesc.Height * resourceDesc.DepthOrArraySize);

            return(totalSizeInBytes);
        }
        // newly override method called 'CreatePage(...)'
        public new static H1GpuMemoryPageSegmented CreatePage(H1GpuMemoryChunk memoryChunk)
        {
            H1GpuMemoryPageSegmented newPage = new H1GpuMemoryPageSegmented(memoryChunk);

            // create resource encapsulated GPU API layer
            H1HeapType       heapType = H1HeapType.Unknown;
            H1ResourceStates usage    = H1ResourceStates.Invalid;

            H1GpuResourceDesc resourceDesc = new H1GpuResourceDesc();

            resourceDesc.Alignment          = 0;
            resourceDesc.Height             = 1;
            resourceDesc.DepthOrArraySize   = 1;
            resourceDesc.MipLevels          = 1;
            resourceDesc.Format             = H1PixelFormat.Unknown;
            resourceDesc.SampleDesc.Count   = 1;
            resourceDesc.SampleDesc.Quality = 0;
            resourceDesc.Layout             = H1TextureLayout.RowMajor;

            // CPU-writable
            if (newPage.Owner.Type == H1GpuMemoryType.GpuWritable)
            {
                heapType = H1HeapType.Default;

                resourceDesc.Width = Convert.ToUInt32(H1GpuMemoryPageSize.GpuWritablePageSize);
                resourceDesc.Flags = H1ResourceFlags.AllowUnorderedAccess;
                resourceDesc.Flags = H1ResourceFlags.Unknown;

                usage = H1ResourceStates.UnorderedAccess;
            }

            // GPU-writable
            else if (newPage.Owner.Type == H1GpuMemoryType.CpuWritable)
            {
                // cpu writable is mainly for upload page (it could be 'readback' property)
                heapType = H1HeapType.Upload;

                resourceDesc.Width = Convert.ToUInt32(H1GpuMemoryPageSize.CpuWritablePageSize);
                resourceDesc.Flags = H1ResourceFlags.Unknown;

                usage = H1ResourceStates.GenericRead;
            }

            // @TODO - handle 'Readback' heap type
            else
            {
                // invalid type is assigned!
                return(null);
            }

            // create new page resource
            //if (!newPage.Resource.CreateResource(heapType, resourceDesc, usage))
            {
                return(null);
            }

            // initialize the properties of the gpu memory page
            Int32 segmentCounts = -1;

            // calculate segment counts
            // @TODO - need to consider further for choosing segment counts (kinda magic number)
            if (newPage.Owner.Type == H1GpuMemoryType.GpuWritable)
            {
                segmentCounts = 1 << 8; // 256 segments
                newPage.m_PageSegmentCounts = segmentCounts;
                newPage.m_PageSegmentSize   = Convert.ToInt32(H1GpuMemoryPageSize.GpuWritablePageSize) / newPage.m_PageSegmentCounts;
            }

            else if (newPage.Owner.Type == H1GpuMemoryType.CpuWritable)
            {
                segmentCounts = 1 << 10; // 1024 segments
                newPage.m_PageSegmentCounts = segmentCounts;
                newPage.m_PageSegmentSize   = Convert.ToInt32(H1GpuMemoryPageSize.CpuWritablePageSize) / newPage.m_PageSegmentCounts;
            }

            if (!SetupPageSegments(newPage, segmentCounts))
            {
                return(null);
            }

            return(newPage);
        }